Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Trie-Based Autocomplete

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Trie-Based Autocomplete. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Whatnot Search needs fast prefix suggestions while a user types in the Browse experience. Given a list of unique lowercase product or seller names, build a Trie and return up to limit matching names in lexicographic order.

Implement autocomplete(words, prefix, limit). The function should return an empty list when no word starts with prefix, and it should not return more than limit results.

Formal Specification

  • Input: words, a list of unique lowercase strings; prefix, a lowercase string; and limit, a positive integer.
  • Output: A list of at most limit strings from words that begin with prefix, ordered lexicographically.
  • The prefix itself should be returned only if it appears in words.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(word) <= 50
  • 0 <= len(prefix) <= 50
  • 1 <= limit <= 100
  • Words contain only lowercase English letters
  • All words are unique

Function Signature

def autocomplete(words, prefix, limit):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output