Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Trie-Based Autocomplete
00:00
5 left

Trie-Based Autocomplete

MediumPython

Problem

A search system 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):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.