Code RoomTrie prefix match length
EasyPrep Room Coding #4746

Trie prefix match length

CodingAlgorithms & data structuresEntry–Mid~14 min

A documentation site holds every searchable term in a prefix tree, so the suggestion list is found by walking down from the root one typed character at a time. The list narrows with each keystroke and goes empty the moment a character has no branch to follow. The support team wants to know where a visitor's query left the tree. You get indexed_terms, the stored terms, and typed, what the visitor entered. Return the largest count of leading characters of typed that still match, meaning the largest k between 0 and the length of typed for which at least one indexed term begins with the first k characters of typed. Return 0 when the very first character already matches nothing, when typed is empty, or when the index holds no terms. Indexed terms are non empty and matching is case sensitive.

Implement
typeahead_match_depth(indexed_terms: list[str], typed: str) → int
Examples
in[["config","connect","console"],"conf"]out4
in[["config","connect","console"],"connx"]out4
in[["go"],"gopher"]out2
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 14 min
InputExpectedGot
[["config","connect","console"],"conf"]4not run yetsample
[["config","connect","console"],"connx"]4not run yetsample
[["go"],"gopher"]2not run yetsample