Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- A. 8
- B. 6
- C. 3
- D. 7
Correct answer: D. 7
The distinct subsequences of 'aba' are '', a, b, ab, ba, aa and aba: 7 (the DP doubles per character and subtracts the count before the previous same letter). Doubling without subtracting gives 8, excluding the empty string gives 6, and listing only the length-2 subsequences (ab, ba, aa) gives 3.
More Coding Interview Problems questions
- Problem: A binary string "1100101011" is to be changed so that it alternates (no two adjacent characters are equal). Each operation flips…
- Problem: Count the ordered pairs of words (i, j) with i ≠ j in ["abc", "cba", "bca", "abc", "cab", "xyz"] such that the second word is the…
- Problem: For the string "AABABBA" and k = 1, you may replace at most one character with any other letter. What is the length of the…
- Problem: Count the substrings of length 3 in "xyzzaz" that contain three distinct characters.
- Problem: Min-cost climbing: cost = [10, 15, 20, 5, 1]. You may start at index 0 or 1, and from index i you can pay cost[i] to move up 1 or…
- Problem: Count the sequences of steps from {1, 2, 5} that sum to exactly 10 (the order of steps matters).