Longest Spoken Prefix
Updated
Problem statement
A voice agent receives a sentence wantToSpeak from a language model and reads it aloud. The sentence is always correct. spokenText is a transcript of what the agent has actually said so far. While speaking, the agent sometimes repeats itself: right after saying a character, a word or a short phrase, it may say it again. It may also be cut off before it finishes. It never skips or replaces anything it was supposed to say.
Return the part of wantToSpeak that has been spoken: the longest prefix of wantToSpeak whose characters all appear in spokenText in the same order. They do not have to be next to each other in spokenText; repeated characters may sit between them. Then remove a trailing space from that prefix, if it ends with one. If not even the first character of wantToSpeak appears, return the empty string.
Characters are compared exactly, so an uppercase letter never matches its lowercase form. A last word that was only partly spoken keeps the characters that were said. Your function must follow this definition for every spokenText allowed by the constraints.
Function
longestSpokenPrefix(wantToSpeak: String, spokenText: String) → StringExamples
Example 1
wantToSpeak = "I can help you book an air ticket"spokenText = "I can can help you book"return = "I can help you book"The agent says can twice. Reading spokenText from left to right, the characters of I can help you book all appear in order: the extra can is simply skipped. The next character of wantToSpeak is the space before an, and spokenText has nothing left after book, so the prefix stops there.
Example 2
wantToSpeak = "I can help you book an air ticket"spokenText = "I can can"return = "I can"After I can the next character of wantToSpeak is the h of help, and no h remains in spokenText. The matched prefix is I can with a trailing space, which is removed, so the answer is I can. A longest-common-subsequence answer would be I can an: those characters do appear in order in both texts, but they are not a prefix of wantToSpeak.
Example 3
wantToSpeak = "Your table is ready, see you soon!"spokenText = "Your tab table is is ready, see y"return = "Your table is ready, see y"The agent restarts table after saying tab, repeats is, and is cut off in the middle of you. Skipping the repeated pieces, every character of Your table is ready, see y appears in order. The partly spoken last word keeps the characters that were said, so the answer ends with y.
Constraints
1 <= wantToSpeak.length <= 10000 <= spokenText.length <= 3000wantToSpeakandspokenTextcontain only English letters, digits, spaces, and the punctuation marks.,'?!.wantToSpeakdoes not start or end with a space, and its words are separated by single spaces.
More ElevenLabs interview questions
Similar questions at other companies
- Missing WordsVanguard · OAVanguard interview questions
- Palindrome After At Most One DeletionAdobe · PHONE SCREENAdobe interview questions
- Shortest Way to Form a Target StringPinterest · PHONE SCREENPinterest interview questions
- Minimum Swaps to Make PalindromeAmazon · OA
- Wildcard Matching with StarsConfluent · PHONE SCREENConfluent interview questions
- Text Pattern Matching with StarsHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Smallest Merge of Two StringsCapital One · OACapital One interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions