PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Pinterest

Implement trie-based autocomplete

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding and implementation of trie data structures for prefix-based retrieval, testing competencies in string algorithms, data structures, and algorithmic time/space complexity; Category: Coding & Algorithms.

  • hard
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Implement trie-based autocomplete

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

## Problem Design an autocomplete data structure using a **trie**. You must support: 1. `insert(word: string) -> void` 2. `search(word: string) -> bool` (exact match) 3. `startsWith(prefix: string) -> bool` 4. `suggest(prefix: string, limit: int) -> list[string]` returning up to `limit` words that start with `prefix`. ## Notes / Constraints - Words contain lowercase English letters `a-z`. - `suggest` may return results in any deterministic order (e.g., lexicographic) as long as it is consistent. - Aim for typical trie complexities: O(L) for insert/search where L is string length; `suggest` should be proportional to output size (plus traversal). ## Edge cases - Duplicate inserts. - Empty prefix. - Prefix that matches no words.

Quick Answer: This question evaluates understanding and implementation of trie data structures for prefix-based retrieval, testing competencies in string algorithms, data structures, and algorithmic time/space complexity; Category: Coding & Algorithms.

Related Interview Questions

  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)
  • Assign Pins to Shortest Columns - Pinterest (medium)
  • Design Hierarchical Permission Checks - Pinterest (medium)
Pinterest logo
Pinterest
Jan 12, 2026, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
4
0
Loading...

Problem

Design an autocomplete data structure using a trie.

You must support:

  1. insert(word: string) -> void
  2. search(word: string) -> bool (exact match)
  3. startsWith(prefix: string) -> bool
  4. suggest(prefix: string, limit: int) -> list[string] returning up to limit words that start with prefix .

Notes / Constraints

  • Words contain lowercase English letters a-z .
  • suggest may return results in any deterministic order (e.g., lexicographic) as long as it is consistent.
  • Aim for typical trie complexities: O(L) for insert/search where L is string length; suggest should be proportional to output size (plus traversal).

Edge cases

  • Duplicate inserts.
  • Empty prefix.
  • Prefix that matches no words.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Pinterest•More Machine Learning Engineer•Pinterest Machine Learning Engineer•Pinterest Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.