Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Word break

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.

Requirements

  • Words may be reused.

Constraints

  • Lowercase strings.
  • Up to 300 characters and 1,000 words.

Examples

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

advanced

Word break

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.