KatInterview

Key-Value Store Operation Simulator

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationdata-structures

Last updated

Tests ability to use hash maps for constant-time operations and handle state transitions in a key-value store simulation.

You are building a simulator for a key-value storage system. Your task is to process a sequence of operations on an initially empty store and return the results.

You will receive two parallel lists:

  • operations: a list of operation names
  • arguments: a list of argument lists, where arguments[i] contains the parameters for operations[i]

Your function must process each operation in order and collect results into a list.

Operations

"insert" — Arguments: [key, value]

  • Store the key-value pair
  • If the key already exists, replace its value with the new one
  • Append None to the results

"get" — Arguments: [key]

  • Retrieve the value associated with the key
  • Append the value to the results, or None if the key is not present

"delete" — Arguments: [key]

  • Remove the key and its value from the store if it exists
  • If the key is not present, do nothing
  • Append None to the results

Keys and values are both strings. An empty string is a valid key or value.

Constraints

  • 1 ≤ len(operations) ≤ 10⁵
  • len(operations) == len(arguments)
  • All keys and values are strings with length ≤ 100
  • Each operation name is one of "insert", "get", or "delete"

Examples

Example 1

Input: simulate_key_value_store(['insert', 'get', 'insert', 'get', 'delete', 'get'], [['name', 'Ada'], ['name'], ['name', 'Grace'], ['name'], ['name'], ['name']])

Output: [None, 'Ada', None, 'Grace', None, None]

Hints

Hint 1

A hash map provides average O(1) time for lookups, insertions, and deletions.

Hint 2

Use dictionary methods that handle missing keys gracefully rather than checking existence explicitly.

Hint 3

The result list must have exactly one entry per operation, with None representing both 'no return value' and 'key not found'.