But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
You're building the search box for a bakery's online menu.
The menu has grown to thousands of items: seasonal pies, a dozen kinds of croissant, every macaron flavor you can think of. Nobody wants to scroll through all that. So as a customer types, the search box should suggest every item that starts with what they've typed so far.
Write an Autocomplete class:
For example:
The menu only changes when the head baker dreams up something new, but suggest runs on every keystroke, for every customer. So it's fine to do some work up front in the constructor if it makes suggest faster.
Menu items only contain lowercase letters and spaces, and no two items have the same name.
This is often called the autocomplete or prefix search problem.
What if one menu item is the start of another, like "tart" and "tartlet"? suggest with the prefix "tart" should return both.
We're only matching the start of each item. "chocolate chip cookie" contains "cookie," but it doesn't start with it, so the prefix "cookie" shouldn't suggest it.
What should an empty prefix return? Every string starts with the empty string, so we should get back the whole menu, in alphabetical order.
Don't sort the suggestions every time suggest runs. We can get them out of our data structure already in alphabetical order.
We can make suggest run in O(p + mk) time, where p is the length of the prefix, m is the number of suggestions we return, and k is the length of the longest menu item. Notice there's no n (the number of menu items) in there. The cost of a search shouldn't depend on how big the menu is!
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Say the menu has n items, the longest item is k characters long, the prefix is p characters long, and suggest returns m suggestions.
Building the trie in the constructor takes O(nk) time and O(nk) space, since we add at most one node for each character of each item. (Items that share a prefix share nodes, so in practice it's usually less.)
Each call to suggest takes O(p + mk) time. Walking down to the prefix's node takes O(p). Every node below that node is part of at least one suggestion, so the depth-first search does work proportional to the total length of the suggestions, which is at most O(mk). We'd have to spend that much time just building the output, so we can't do any better on that part.
suggest uses O(mk) space for the suggestions it returns, plus O(k) for the call stack and the characters on the current path.
The big win: there's no n in suggest's time cost. When a customer types "croi," we follow four nodes down to the end of the prefix, then walk the handful of nodes below it to spell out "croissant." That's the same amount of work whether the menu has 50 items or 50,000.
Is a trie the only good answer? Nope! You could also sort the menu once, then use binary search to find the first item that comes at or after the prefix alphabetically, and walk forward from there until the items stop matching. That takes O(p lg n + mk) time per call. Searches are a bit slower than with a trie, but a sorted list usually uses much less memory, since every trie node carries its own collection of children. Talking through tradeoffs like this is a great move in an interview.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor