PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

Support insert, search, startsWith, and lexicographic suggest operations over lowercase words. Return one output per operation.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([['insert','apple'], ['insert','app'], ['search','app'], ['startsWith','ap'], ['suggest','app',5]],)

Expected Output: [None, None, True, True, ['app', 'apple']]

Explanation: Insert/search/suggest.

Input: ([['suggest','',2], ['insert','bat'], ['insert','bar'], ['suggest','ba',1]],)

Expected Output: [[], None, None, ['bar']]

Explanation: Empty trie then limited suggestions.

Input: ([['insert','same'], ['insert','same'], ['suggest','s',3]],)

Expected Output: [None, None, ['same']]

Explanation: Duplicate insert is idempotent.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

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.

Related Coding Questions

  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)