Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Haiku Substring With Syllables

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Haiku Substring With Syllables. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Faire's product and content surfaces sometimes process short text where a poetic 5-7-5 structure is meaningful. Given a whitespace-separated string, return the first contiguous substring whose three consecutive phrases contain exactly 5, 7, and 5 syllables.

A substring must begin and end on word boundaries. Search from left to right by starting word index. Return the candidate with the smallest starting index, and return the empty string if no candidate exists.

For this problem, count syllables in each word using this deterministic rule: lowercase the word, count contiguous groups of aeiouy, subtract one if the word ends in e and has more than one group, and return at least one syllable per word. Words contain only alphabetic characters and optional apostrophes.

Formal Specification

  • Input: text, a string containing zero or more whitespace-separated words.
  • Output: the earliest 5-7-5 contiguous substring as a normalized space-separated string, or "" if none exists.

Constraints

  • 0 <= len(text) <= 200,000
  • text contains at most 100,000 words
  • Each word has length between 1 and 30 characters
  • Words contain alphabetic characters and optional apostrophes
  • Every word contributes at least one syllable

Function Signature

def first_haiku_substring(text):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output