KatInterview

Weighted Prefix Suggestion Engine via Command Replay

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designtrieprefix-searchheap

Last updated

Replay a list of add and search commands on a weighted word store, returning the top-k prefix matches ranked by weight, then alphabetically.

Weighted Prefix Suggestions

You are building the core of a search-suggestion feature. It keeps a collection of words, each with an integer score, and answers prefix queries by returning the best-scoring matches.

Instead of writing a class, you implement a single function that replays a list of commands against an initially empty collection and reports what each command produced.

Commands

Command i is named by commands[i] and its arguments are in params[i].

  • "add_word" with params[i] = [word, weight]

    • Stores word with the given integer weight.
    • If word is already stored, its weight is replaced by the new value. The weights are not summed, and no duplicate entry is created. The new weight may be lower or higher than the old one.
    • Produces None in the output.
  • "search" with params[i] = [prefix, k]

    • Considers every stored word that starts with prefix. A word counts as starting with itself, and the empty prefix "" matches every stored word.
    • Returns at most k of those words, ranked by:
      1. weight, highest first;
      2. then, among equal weights, ordinary lexicographic (string) order, smallest first.
    • If fewer than k words match, return all of them in that order.
    • If nothing matches, return an empty list.
    • Searching never modifies the collection.

Output

Return a list with one entry per command, in the same order. The entry is None for "add_word", and the list of suggested words for "search".

Each search must reflect all add_word commands that came before it, and only those.

Constraints

  • len(commands) == len(params).
  • Every command name is either "add_word" or "search".
  • Words and prefixes consist of strings of characters. Prefixes may be empty, and words are non-empty.
  • Weights are integers (they may be negative or zero).
  • k is a positive integer with no fixed upper bound, so it may exceed the number of matches.
  • The total number of characters across all added words is at most 10^6.

Examples

Example 1

Input: run_suggestion_commands(['add_word', 'add_word', 'add_word', 'add_word', 'search'], [['apple', 10], ['apply', 15], ['app', 5], ['bat', 20], ['app', 2]])

Output: [None, None, None, None, ['apply', 'apple']]

Hints

Hint 1

A trie is a natural fit for prefix matching: following the prefix character by character leads to a node whose subtree is exactly the set of matching words.

Hint 2

Keep the words and their weights in separate places: the trie decides which words match, and a dictionary from word to current weight decides how they rank. This makes re-adding a word with a lower weight trivial.

Hint 3

Rank matches with the sort key (-weight, word). When only k results are needed, heapq.nsmallest with that key avoids sorting everything.