Code RoomHandshake preamble match
EasyPrep Room Coding #4790

Handshake preamble match

CodingNetworking & APIsEntry–Mid~15 min

A serial capture tool checks that every log a device sends opens with the handshake preamble its firmware promises. The preamble is matched from the very first character of the stream, never from further along. A '?' in the preamble stands for one character the firmware is free to pick, a revision byte for instance, so it accepts whatever the stream holds in that spot. A '?' in the stream is an ordinary character with no special meaning. Given stream and preamble, return the index of the first position where the stream stops following the preamble. When the stream runs out before the preamble is finished, that position is the length of the stream. Return -1 when the whole preamble is followed, even when the stream carries on well past it. Anything follows an empty preamble.

Implement
preamble_break_index(stream: str, preamble: str) → int
Examples
in["AB12-READY","AB??-"]out-1
in["AB12*READY","AB??-"]out4
in["AB1","AB??"]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
["AB12-READY","AB??-"]-1not run yetsample
["AB12*READY","AB??-"]4not run yetsample
["AB1","AB??"]3not run yetsample