Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: Count the ways to segment "catsanddog" into a sequence of words, each taken from the dictionary {cat, cats, and, sand, dog} (every character used exactly once, in order).
- A. 3
- B. 1
- C. 2
- D. 4
Correct answer: C. 2
Word-break counting: dp[i] sums the segmentations of each prefix that end with a dictionary word. Stopping at the first valid segmentation finds only one.
More Coding Interview Problems questions
- Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- 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…