Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code.
Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code.
from itertools import product
print(sum(1 for t in product('ab', repeat=4) if t == t[::-1]))What is printed?- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 4
A length-4 palindrome is determined by its first two letters, giving 2^2 choices.
More Coding Interview Problems questions
- Problem: In how many ways can you climb 12 stairs taking 1, 2 or 3 steps at a time (order matters)? Run the DP. python ways = [1] + [0] 12…
- Problem: What is the length of the longest subarray of [1, 2, -3, 3, -1, -2] whose sum is zero? Run the prefix-sum code. python a = [1, 2…
- Problem: How many integers in [1, 100] are sums of two positive cubes? Run the code. python print(len({a 3 + b 3 for a in range(1, 5) for…
- Problem: How many distinct sums of two different elements of [1, 2, 4, 8] are there? Run the code. python from itertools import…
- Problem: How many ways are there to place 2 non-attacking rooks on a 4 x 4 board? Run the code. python from itertools import combinations…
- Problem: How many integers in [1, 1000] have exactly three distinct decimal digits? Run the code. python print(sum(1 for x in range(1…