Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Build Data Structures Under Time Pressure

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

Your question is Build Data Structures Under Time Pressure. 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

Pinterest Search needs to return up to k autocomplete suggestions for a lowercase prefix. Given a collection of searchable terms, build a trie and return the matching terms in lexicographic order.

Implement autocomplete(words, prefix, k). Each term may be inserted once, and suggestions must contain complete terms that start with prefix. Return fewer than k terms when fewer matches exist.

Formal Specification

  • Input: words, a list of lowercase strings; prefix, a lowercase string; and k, a positive integer.
  • Output: A list of at most k distinct strings beginning with prefix, sorted in lexicographic order.
  • The input terms are unique and contain only characters from a through z.

Constraints

  • 1 <= len(words) <= 10^5
  • 1 <= len(word) <= 50
  • 0 <= len(prefix) <= 50
  • 1 <= k <= 100
  • Words contain only lowercase English letters
  • The input terms are unique
  • The total number of characters across all words is at most 5 * 10^5

Function Signature

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