Rank Slash Command Autocomplete Suggestions With a Trie

Last updated: November 29, 2026

Quick Overview

Build an autocomplete for chat slash commands: `record(command)` logs a use, and `suggest(prefix)` returns up to k commands starting with the prefix, ranked by usage count descending, then alphabetically.

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

0

0

0 solved


Build an autocomplete for chat slash commands: `record(command)` logs a use, and `suggest(prefix)` returns up to k commands starting with the prefix, ranked by usage count descending, then alphabetically.

Chat interfaces like ChatGPT surface commands and saved prompts as the user types, and suggestions must return in a few milliseconds per keystroke. OpenAI interviewers look for a trie, a clear choice between computing results at query time and caching top-k per node, and an argument for why the cache stays correct as counts change.

What the Interviewer Expects
  • Use a trie keyed by characters
  • Discuss query-time DFS versus caching top-k at each node
  • Keep the cache correct when an existing command's count increases
  • Define the ranking and tie-break explicitly
  • Analyze cost per record and per suggest
Key Topics to Cover
trie
prefix search
top-k caching
autocomplete
ranking
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
  • Counts should decay so recent usage matters more: how do you maintain the ranking?
  • Support deleting a command: why does the cached top-k approach get harder?
  • Personalize suggestions per user while sharing a global trie.
  • Serve this for 100 million users: where does the trie live?
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(cmd) increments the usage count of cmd.
  • suggest(prefix) returns at most k commands with that prefix, sorted by count descending, then lexicographically.
  • Unknown prefix returns `[]...
High-Level Design

Because reads dominate, store at each trie node a sorted list of the best k (-count, command) pairs in its subtree. suggest then walks the prefix and returns the cached list in O(∣p∣+k)O(|p| + k). `re...


Submit Your Answer
Markdown supported