KatInterview

Simulated Word Store with Prefix Query

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designtriestringssimulation

Last updated

Tests trie-style prefix counting logic via operation replay, with optional case-insensitive and trailing-digit normalization flags.

Simulated Word Store with Prefix Query

You are building a simple in-memory word store that supports two operations, processed in order as a sequence of commands.

Operations

  • "insert" — add a word to the store. This operation produces no output (yields None in the result list).
  • "count_with_prefix" — given a prefix string, count how many words currently in the store begin with that prefix. This operation yields an integer in the result list.

You receive two parallel lists:

  • ops: a list of operation names, each either "insert" or "count_with_prefix".
  • args: a list of single-element lists, where args[i] holds the argument for ops[i].

Process every operation in order and return a list of results in the same order. Insert results are None; query results are integers.

Normalization flags

Two optional boolean flags change how words and prefixes are stored and matched:

  • case_insensitive — when True, apply Unicode case-folding (str.casefold()) to every word before inserting and to every prefix before querying.
  • strip_trailing_digits — when True, remove any trailing run of decimal digits from a word or prefix before inserting or querying (e.g. "item42" becomes "item"; "4ever" stays "4ever").

When both flags are True, trailing digits are stripped first, then case-folding is applied. The same transformation must be used for both inserts and queries — normalization is applied to the argument before it is stored or looked up.

Duplicate inserts count separately. Inserting the same (post-normalization) word twice means a query for its prefix returns at least 2.

Prefix matching uses str.startswith. An empty prefix matches every stored word.

Constraints

  • 1 ≤ len(ops) == len(args) ≤ 1 000
  • Each args[i] is a list containing exactly one non-empty string.
  • Every string consists of printable Unicode characters.
  • ops[i] is either "insert" or "count_with_prefix".
  • case_insensitive and strip_trailing_digits are booleans (default False).
  • The result list has the same length as ops. Insert positions are None; query positions are non-negative integers.

Examples

Example 1

Input: prefix_search(['insert', 'insert', 'count_with_prefix', 'count_with_prefix', 'insert', 'count_with_prefix'], [['apple'], ['app'], ['app'], ['apple'], ['application'], ['ap']])

Output: [None, None, 2, 1, None, 3]

Example 2

Input: prefix_search(['insert', 'count_with_prefix', 'insert', 'count_with_prefix'], [['Report123'], ['report'], ['REPORT456'], ['report'], True, True])

Output: [None, 0, None, 0]

Hints

Hint 1

Keep a plain list of normalized words. For each count_with_prefix query, scan the list and count words where word.startswith(normalized_prefix) is True — no fancy data structure needed at this scale.

Hint 2

The normalization flags must be applied to both inserts and queries using exactly the same helper function. Write it once and call it in both branches.

Hint 3

Watch the order of normalization steps: strip trailing digits with re.sub(r'\d+$', '', s) before applying casefold(). An empty string after stripping is valid — just store or query it as-is.