Fuzzy Dictionary Search Within K Edits Using a Trie
Last updated: November 29, 2026
Quick Overview
Given a dictionary and a query word, return every dictionary word whose Levenshtein distance to the query is at most k, without computing a full DP for each word separately.
0
0
0 solved
Given a dictionary and a query word, return every dictionary word whose Levenshtein distance to the query is at most k, without computing a full DP for each word separately.
A Google onsite problem close to real spelling correction in Search and Gboard. The interviewer wants the trie plus rolling DP row technique, where each trie edge extends one row of the edit-distance table, and pruning when the row minimum exceeds k.
What the Interviewer Expects
- Explain the per-word DP baseline and its cost
- Share DP rows across words with a common prefix by walking the trie
- Compute each child row from its parent row in O(len(query))
- Prune a subtree when the minimum of a row exceeds k
- Collect words at end nodes whose last cell is at most k
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does a BK-tree compare for this problem?
- How would you rank candidates by keyboard distance and frequency?
- How would you add transpositions (Damerau distance)?
- How would you shard the dictionary across machines?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: words, a dictionary of lowercase strings, query, and a small integer k. Output: a sorted list of (word, distance) for words with distance at most k....
High-Level Design
The edit distance between query and word w is the last cell of a DP table whose row i depends only on row i - 1 and character w[i-1]. Words sharing a prefix share the first rows. So walk the...