FastPrepLongest Spoken Prefix
Problems/Longest Spoken Prefix

Longest Spoken Prefix

ElevenLabs logoElevenLabs● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A candidate for a backend-leaning full-stack engineer role at ElevenLabs described the live-coding round of an onsite. The task was framed as a voice agent. A language model produces the text the agent should say, and that text is always right. The agent's spoken version may contain stutters, meaning characters, words or phrases said again, but it never skips or swaps words. The function returns the part of the intended text that has been spoken. The round started with reviewing a junior engineer's longest-common-subsequence solution. It went out of bounds in its table and, for a short utterance, returned characters that are not a prefix of the intended text. Then the candidate was asked for a correct version. They built it by scanning both texts from left to right and skipping spoken characters that do not match. The report includes two worked examples, used here as Examples 1 and 2. It gives no signature, character set or limits.

Here is what we chose. The answer is the longest prefix of the intended text whose characters appear in order in the spoken text, which is what the left-to-right scan finds. A trailing space is removed from that prefix. A last word that was only partly said keeps its spoken characters instead of being dropped. Characters are compared exactly, so case matters. The texts use English letters, digits, single spaces in the intended text and a few punctuation marks. The code-review part is not tested and appears only as a known pitfall in the editorial. The third example and all limits are ours.

Our version is about 85% the same as the reported question.

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) → String

Examples

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 <= 1000
  • 0 <= spokenText.length <= 3000
  • wantToSpeak and spokenText contain only English letters, digits, spaces, and the punctuation marks . , ' ? !.
  • wantToSpeak does not start or end with a space, and its words are separated by single spaces.

More ElevenLabs interview questions

  • Dubbing Review Status PropagationONSITE INTERVIEW · Seen May 2025
  • Lowest-Cost Shirt Color ScheduleOA · Seen Oct 2026
  • Minimum Magic Stones After MergesOA · Seen Oct 2026
  • Next Number with Disjoint DigitsOA · Seen Oct 2026

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
All ElevenLabs interview questionsSee ElevenLabs hiring insights
Saved
class Solution {
    public String longestSpokenPrefix(String wantToSpeak, String spokenText) {
        // Write your code here.
    }
}
Checking account…