KatInterview

Replay Commands on a Fixed-Bucket Key-Value Store

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-tableseparate-chainingsimulation

Last updated

Simulate a hand-built key-value store with a fixed bucket count by replaying a command log, handling collisions and overwrites correctly without a built-in dictionary.

Replay a Key-Value Store Command Log

You are building a small key-value store from scratch and must replay a log of commands against it. The store uses a fixed number of buckets, and several different keys may end up in the same bucket. Your store must stay correct when that happens.

Write a function that takes two parallel lists, commands and payloads. The command at position i is paired with the argument list at position i in payloads. Process the commands in order and return one result per command.

Commands

  • "HashTable" or "init": payload is [bucket_count]. Creates a new, empty store with that many buckets. The result for this command is None.
  • "put": payload is [key, value]. Stores value under key. If key is already present, its old value is replaced, so each key appears at most once. The result is None.
  • "get": payload is [key]. The result is the value currently stored under key, or -1 if the key was never stored.

Rules

  • Keys are never null. They may be integers or strings, and both kinds can appear in the same log.
  • Two keys are the same only if they are equal. Keys that land in the same bucket must not overwrite or hide each other.
  • If bucket_count is less than 1, treat it as 1.
  • Bucket selection should come from the key's hash reduced modulo the number of buckets. The result must be a valid, non-negative index.
  • You must not use a built-in dictionary, set, or other built-in hash map to hold the entries. Lists are fine.
  • Average put and get cost should be O(1) when keys are spread reasonably across buckets.
  • The first command in every log creates the store.

Output

Return a list with exactly one entry per command, in the same order as the input. Entries for creation and put commands are None. Entries for get commands are the stored value, or -1 if the key is absent.

Constraints

  • commands and payloads have the same length.
  • Each get or put happens after the store has been created.
  • Keys are non-null integers or strings.
  • Values can be any JSON-like value (for example integers or strings).
  • No built-in hash map or dictionary may be used for storage.

Examples

Example 1

Input: replay_store_commands(['HashTable', 'put', 'put', 'get', 'get'], [[3], ['a', 10], ['b', 20], ['a'], ['c']])

Output: [None, None, None, 10, -1]

Example 2

Input: replay_store_commands(['HashTable', 'put', 'put', 'get'], [[2], [1, 'first'], [1, 'second'], [1]])

Output: [None, None, None, 'second']

Example 3

Input: replay_store_commands(['HashTable', 'put', 'put', 'get', 'get'], [[4], [0, 'zero'], [4, 'four'], [0], [4]])

Output: [None, None, None, 'zero', 'four']

Hints

Hint 1

Keep a list of buckets, where each bucket is a list of [key, value] pairs. Choose a bucket with hash(key) % number_of_buckets.

Hint 2

Before adding a new pair in put, scan the chosen bucket for an equal key. If found, update its value in place instead of appending a duplicate.

Hint 3

For get, scan only the one bucket and compare keys with equality. Return -1 if the scan ends without a match. Do not forget to clamp the bucket count to at least 1.