Ranked Autocomplete for Defined Terms While Drafting

Last updated: November 29, 2026

Quick Overview

Build a structure that records how often each defined term is used in a draft and, given a prefix, returns the k most used terms that start with it, ties broken alphabetically.

Harvey
Coding & Algorithms
Software Engineer
Harvey
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Build a structure that records how often each defined term is used in a draft and, given a prefix, returns the k most used terms that start with it, ties broken alphabetically.

Harvey's drafting experience inside Word and its own editor benefits from suggesting the exact defined term (for example 'Indemnifying Party') as a lawyer types. The interviewer wants a trie with cached top k, a clear statement of the read and write costs, and awareness of when the cache goes stale.

What the Interviewer Expects
  • Choose a trie keyed by characters
  • Cache the top k per node so reads are O(prefix length)
  • Update caches along the path on each use
  • Make the case and tie rules explicit
  • Explain why decrements break the simple cache
Key Topics to Cover
trie
prefix search
top-k caching
data structure design
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support removing a term or decreasing its count?
  • How would you rank by recency as well as frequency?
  • Could you match on any word inside the term, not only the start?
  • How do you keep memory reasonable for a firm-wide term dictionary?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Operations: record_use(term, times) and suggest(prefix). Case-insensitive. Return up to k terms ordered by count descending, then alphabetically. Reads happen on every keystroke, so they must be f...

High-Level Design

A trie where each node stores its children and a cached list of the best k terms in its subtree. On record_use, walk the term's path and, at each node, merge the term into that node's top list and k...


Submit Your Answer
Markdown supported

Related Questions