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.