FastPrepStreaming Inference Stop Sequence Detection

Streaming Inference Stop Sequence Detection

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

An inference stream arrives as an ordered array of text chunks. Detect the configured nonempty stopSequences while consuming the characters in order, and return the generated text before the selected stop.

  • Matching is case-sensitive. A stop may span any number of chunks, and empty chunks do not reset matching state.
  • Stop immediately when any stop sequence finishes. If several finish on the same character, choose the longest one, which starts earliest.
  • Exclude the entire selected stop and ignore all later characters.
  • If no stop completes, return all input text, including an unfinished stop prefix at the end. An empty stop list never stops the stream.

This exercise uses text stop sequences, including multi-character markers such as <END>.

Function

truncateAtStop(chunks: String[], stopSequences: String[]) → String

Examples

Example 1

chunks = ["Hello <EN","D>ignored"]stopSequences = ["<END>"]return = "Hello "

The stop sequence spans the two chunks and is excluded from the returned text.

Example 2

chunks = ["abcd"]stopSequences = ["abcd","bc"]return = "a"

bc completes after c, before the longer abcd stop can complete. Detection uses the earliest completion, not the earliest start.

Example 3

chunks = ["x","abc"]stopSequences = ["bc","abc"]return = "x"

Both stops complete on c. The longer abc begins earlier, so it is the selected stop.

Constraints

  • 0 <= chunks.length <= 1000; total chunk length is at most 100000.
  • 0 <= stopSequences.length <= 200.
  • Every stop has length between 1 and 200, and their total length is at most 4000.
  • All characters are printable ASCII, with codes from 32 through 126.

More Microsoft problems

See Microsoft hiring insights
public String truncateAtStop(String[] chunks, String[] stopSequences) {
    // Write your code here
}
chunks["Hello <EN","D>ignored"]
stopSequences["<END>"]
expected"Hello "
Checking account…