FastPrepFind the First Contiguous Haiku

Find the First Contiguous Haiku

Faire logoFaire● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a sentence, a dictionary of normalized words, and their positive syllable counts, return the first contiguous segment that forms a 5-7-5 haiku.

Split the sentence on ASCII whitespace. For dictionary lookup, lowercase each token and strip surrounding punctuation while preserving internal apostrophes. Return three strings made from the original tokens, preserving spelling and punctuation. Choose the smallest starting token index whose cumulative syllable boundaries are exactly 5, 12, and 17. Return an empty array when no such segment exists.

Function

findFirstHaiku(sentence: String, words: String[], syllables: int[]) → String[]

Examples

Example 1

sentence = "Noise, a quiet old pond a frog jumps in sound again."words = ["noise","a","quiet","old","pond","frog","jumps","in","sound","again"]syllables = [2,1,2,1,1,1,1,1,1,2]return = []

This input does not reach a final five-syllable line, so no complete haiku exists and the returned array is empty.

Example 2

sentence = "Bright sun over hills soft rain touches green leaves calm winds carry birds home"words = ["bright","sun","over","hills","soft","rain","touches","green","leaves","calm","winds","carry","birds","home"]syllables = [1,1,2,1,1,1,2,1,1,1,1,2,1,1]return = ["Bright sun over hills","soft rain touches green leaves calm","winds carry birds home"]

The three consecutive groups total 5, 7, and 5 syllables.

Constraints

  • 1 <= token count <= 2000
  • words.length == syllables.length
  • Every normalized sentence token appears exactly once in words.
  • Every syllable count is positive.
See Faire hiring insights
public String[] findFirstHaiku(String sentence, String[] words, int[] syllables) {
    // Write your code here.
}
sentence"Noise, a quiet old pond a frog jumps in sound again."
words["noise","a","quiet","old","pond","frog","jumps","in","sound","again"]
syllables[2,1,2,1,1,1,1,1,1,2]
expected[]
Checking account…