Ranked Intent Autocomplete Trie for Agent Builder Search

Last updated: November 29, 2026

Quick Overview

Design a structure that records phrases customers type (such as intent names) and, for any prefix, returns the top k phrases by frequency with ties broken alphabetically. Both record and suggest should be fast.

Sierra
Coding & Algorithms
Software Engineer
Sierra
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Design a structure that records phrases customers type (such as intent names) and, for any prefix, returns the top k phrases by frequency with ties broken alphabetically. Both record and suggest should be fast.

Teams building agents in Sierra's tooling search across many intents, journeys and knowledge articles, and suggestions should surface the most used first. The interviewer checks trie construction, caching top-k at nodes, and reasoning about the read versus write trade-off.

What the Interviewer Expects
  • Build a character trie
  • Store a small sorted top-k list at each node
  • Update every node on the phrase's path when a count changes
  • Make suggest O(prefix length + k)
  • Explain why counts only increasing keeps the cache correct
Key Topics to Cover
trie
top-k
prefix search
precomputation
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
  • What changes if phrases can be deleted or counts can decrease?
  • How would you make suggestions case-insensitive and tolerant of a typo?
  • How would you shard this for millions of phrases across tenants?
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

record(phrase) increments that phrase's count. suggest(prefix) returns up to k phrases starting with the prefix, ordered by count descending then alphabetically. Reads are far more frequent than w...

High-Level Design

A naive trie would DFS the whole subtree on every suggest. Instead each node caches the top k phrases in its subtree. When a phrase's count rises, only nodes on its path can change, and the phrase can...


Submit Your Answer
Markdown supported