Find first 5-7-5 haiku in sentence
Company: Faire
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Find first 5-7-5 haiku in sentence evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= number of words; sentence may be empty or whitespace-only.
- Syllable counts are positive integers; an absent word is an unknown barrier.
- Token normalization: lowercase, strip leading/trailing non-alphanumeric characters, keep interior apostrophes.
- Return the FIRST haiku in left-to-right scan order when several exist.
- Preserve original token casing and punctuation in the returned lines.
Examples
Input: ('An old pond a frog jumps in the water splashes the morning coding', {... syllable dict ...})
Expected Output: ['An old pond a frog', 'jumps in the water splashes', 'the morning coding']
Explanation: Line sums: 1+1+1+1+1=5, then 1+1+1+2+2=7, then 1+2+2=5. A clean 5-7-5 starting at the first token.
Input: ('code', {... syllable dict ...})
Expected Output: None
Explanation: A single one-syllable word can never reach the first line's 5-syllable target.
Input: ('', {... syllable dict ...})
Expected Output: None
Explanation: Empty sentence yields no tokens, so no haiku exists.
Input: ('Hello, An old pond, a frog jumps in the water splashes! the morning coding done', {... syllable dict ...})
Expected Output: ['An old pond, a frog', 'jumps in the water splashes!', 'the morning coding']
Explanation: 'Hello,' is an unknown barrier so the haiku starts at 'An'. Lookups strip the punctuation ('pond,' -> 'pond', 'splashes!' -> 'splashes') but the returned lines keep the original tokens verbatim.
Input: ('An old QUUX pond a frog jumps in the water splashes the morning coding', {... syllable dict ...})
Expected Output: None
Explanation: 'QUUX' is unknown, acting as a barrier inside what would otherwise be a haiku run, so no contiguous 5-7-5 partition is possible.
Input: ("don't go slow we win i love to code all day and see it run fast now", {... syllable dict ...})
Expected Output: ["don't go slow we win", 'i love to code all day and', 'see it run fast now']
Explanation: The apostrophe word 'don't' is looked up directly (interior apostrophe preserved) and counts as 1 syllable; the first valid 5-7-5 run is returned.
Hints
- Split on whitespace to get tokens, but normalize a copy of each token (lowercase + strip outer punctuation) only for dictionary lookups; keep the originals for output.
- Treat any word missing from the dictionary as a barrier: a haiku can never span across it.
- Because the line targets (5, 7, 5) are fixed, you can greedily walk forward from each start, accumulating syllables and bailing out the moment a running sum overshoots its target.
- Scan starts left to right and return on the first start index that yields all three exact line sums; this guarantees the first haiku.