Data Structures & AlgorithmsModerateCAT · TCS NQT
Problem: What is the number of distinct strings of length 4 over the alphabet {a, b} containing at least one 'a' and at least one 'b'?
- A. 16
- B. 14
- C. 6
- D. 15
Correct answer: B. 14
2⁴ = 16 strings minus aaaa and bbbb leaves 14. 16 forgets to remove anything, exactly two of each gives C(4,2) = 6, and removing only one uniform string gives 15.
More Coding Interview Problems questions
- Problem: The string "aabbccddee" is compressed by keeping each character once for every run of length at least two, followed by the count…
- Problem: Calculate the number of ordered pairs (x, y) of integers with 1 ≤ x, y ≤ 20 for which x² + y² is divisible by 5.
- Problem: A lattice path goes from (0,0) to (3,3) using steps right (1,0) and up (0,1), and it must never go above the diagonal (that is…
- Problem: The array [1, 2, 4, 8, 16, 32] stores powers of two. For how many pairs (i < j) is a[i] + a[j] a power of two?
- Problem: A queue of 5 customers arrives with service times 4, 2, 6, 1, 3. A single server processes them in order (first come first…
- Problem: Starting with the list [3, 8, 2, 5], repeatedly take the last two elements, replace them by their difference (larger minus…