Find first 5-7-5 haiku in sentence
FaireSoftware Engineer · Technical ScreenLast updated
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.
Question
Given a sentence containing mixed case and punctuation, and a syllable dictionary mapping lowercase words to their syllable counts, implement:
find_haiku(sentence: str, syllable_dict: dict) -> list[str] | None
Return the first contiguous three-line haiku whose three lines have 5, 7, and 5 syllables respectively, or None if no such haiku exists.
- The three lines must be formed by partitioning a single contiguous subsequence of words into three consecutive segments that sum to 5, 7, and 5 syllables (in that order).
- Treat tokens case-insensitively for dictionary lookups, and strip leading/trailing punctuation when matching keys.
- When returning the three lines, preserve each original token's casing and punctuation (return the words exactly as they appeared in the sentence).
- Aim for O(N) time (e.g., prefix sums + hashing) and state the space complexity.
- Discuss edge cases such as: unknown words (not in the dictionary), apostrophes (e.g.,
"don't"), trailing punctuation, and multiple/irregular spaces.
Explain your approach and its time and space complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.