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).

  1. A. 3
  2. B. 1
  3. C. 2
  4. 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

Exams that ask this topic