Python data engineering interview problem. Difficulty: advanced. Pattern: Dynamic Programming. About 20 minutes. Part of the Pro drill bank.
Decide whether a string can be split into words from a dictionary, reusing words freely. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement word_break(s: str, words: list[str]) -> bool. Return True if s can be cut into one or more consecutive pieces, each of which is in words. A word may be used any number of times. The empty string can always be formed. Strings can have 100 characters or more, so trying every cut recursively without remembering results is too slow.
Input: word_break("abcd", ["ab", "abc", "cd"]) Output: True ab + cd works. Taking the longest word first (abc) leaves d, which is not a word.
Topics: lakebench, python, segmentation, memoization.
More Python interview questions · All interview problems · Learn data engineering
Interview-style drill: Decide whether a string can be split into words from a dictionary, reusing words freely.
Implement `word_break(s: str, words: list[str]) -> bool`. Return `True` if `s` can be cut into one or more consecutive pieces, each of which is in `words`. A word may be used any number of times. The empty string can always be formed. Strings can have 100 characters or more, so trying every cut recursively without remembering results is too slow.