You only have free questions left (including this one).

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 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:

  • Its constructor takes a list of menu items.
  • Its suggest method takes a prefix and returns a list of every menu item that starts with prefix, in alphabetical order.

For example:

autocomplete = Autocomplete([ 'chocolate chip cookie', 'cinnamon roll', 'cherry pie', 'carrot cake', 'chocolate cake', 'cheesecake', 'croissant', 'cherry turnover', ]) autocomplete.suggest('ch') # returns ['cheesecake', 'cherry pie', 'cherry turnover', # 'chocolate cake', 'chocolate chip cookie'] autocomplete.suggest('cherry') # returns ['cherry pie', 'cherry turnover'] autocomplete.suggest('cookie') # returns []

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!

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

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.

  1. Real search boxes only show a handful of suggestions. Say we just want the 5 most popular items that start with the prefix, ranked by how many times each one has been ordered. How could you change the trie so suggest doesn't have to look at every matching item?
  2. Right now, each keystroke starts a brand-new search from the root. When the customer types one more character, how could you pick up where the last search left off?
  3. Customers capitalize however they like ("Cherry Pie," "CHERRY PIE"). How would you make suggestions case-insensitive, while still showing each item's name the way the bakery writes it?
  4. When pumpkin pie comes off the menu after the holidays, how would you remove it from the trie? Which nodes can you safely delete, and which ones do you need to keep?
  5. A customer types "crios." How might you still suggest "croissant"?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

. . .