KatInterview

Word Index with Prefix Queries and Ordered Suggestions

Data Structure DesignAuto-graded · 14 test cases

Last updated

Simulate a command sequence on a word index that supports insert, exact lookup, prefix lookup, and alphabetically ordered, capped autocomplete, built on a trie.

Word Index Simulator

You are building a small in-memory word index. It begins empty and then processes a list of commands in order. Implement run_word_index(commands, params), which executes every command against one shared index and returns the result of each command.

Commands

commands[i] is the name of the i-th command, and params[i] is the list of arguments for it.

CommandArgumentsEffectResult recorded
"insert"[word]Add word to the index. Inserting a word that is already present changes nothing.None
"has_word"[word]Check whether word was inserted exactly as given.True or False
"has_prefix"[prefix]Check whether at least one stored word starts with prefix.True or False
"autocomplete"[prefix, limit]Collect stored words that start with prefix.A list of strings

Autocomplete rules

  • A word counts as starting with prefix if prefix is the word itself or a leading part of it. So a stored word equal to prefix is a match.
  • Return the matching words in ascending dictionary (lexicographic) order. A word comes before every longer word that extends it.
  • Return at most limit words, taking the smallest ones in that order. If fewer words match, return all of them.
  • Each distinct word appears at most once, no matter how many times it was inserted.
  • If nothing matches, or limit is 0, return an empty list.

Empty prefix

An empty prefix "" is allowed in has_prefix and autocomplete. It is a leading part of every word, so autocomplete("", k) lists the smallest k stored words. On an index with no words, has_prefix("") is still True and autocomplete("", k) returns []. A has_word("") query is True only if the empty string was itself inserted.

Output

Return a list with one entry per command, in the same order as commands. The entry is the result from the table above.

Constraints

  • len(commands) == len(params), and every command name is one of the four listed above.
  • The total number of commands is at most 2 * 10^4.
  • Every word and prefix has length between 0 and 100 and contains only lowercase letters 'a' to 'z'.
  • 0 <= limit <= 100.

Examples

Example 1

Input: run_word_index(['insert', 'insert', 'insert', 'insert', 'insert', 'has_word', 'has_word', 'has_prefix', 'has_prefix', 'autocomplete', 'autocomplete', 'autocomplete'], [['apple'], ['app'], ['application'], ['apt'], ['banana'], ['app'], ['appl'], ['appl'], ['bat'], ['app', 2], ['app', 5], ['z', 3]])

Output: [None, None, None, None, None, True, False, True, False, ['app', 'apple'], ['app', 'apple', 'application'], []]

Example 2

Input: run_word_index(['insert', 'insert', 'insert', 'insert', 'has_word', 'has_word', 'has_prefix', 'autocomplete', 'autocomplete', 'autocomplete'], [['car'], ['cart'], ['cat'], ['car'], ['ca'], ['cart'], ['car'], ['ca', 10], ['car', 1], ['cat', 0]])

Output: [None, None, None, None, False, True, True, ['car', 'cart', 'cat'], ['car'], []]

Example 3

Input: run_word_index(['has_word', 'has_prefix', 'autocomplete', 'insert', 'has_word', 'has_prefix', 'autocomplete'], [['a'], [''], ['', 5], ['a'], ['a'], [''], ['', 10]])

Output: [False, True, [], None, True, True, ['a']]

Hints

Hint 1

Store words in a tree where each edge is one letter and each node records whether a stored word ends there. Exact lookup and prefix lookup then differ only in that final flag.

Hint 2

Write one helper that walks a string from the root and returns the node reached, or nothing if the path breaks. All three query types can use it.

Hint 3

For autocomplete, run a DFS from the prefix node. Report a node's own word before descending, and visit children in alphabetical order. That visiting order is already sorted, so stop as soon as you have `limit` words.