KatInterview

Build a Persistent Key-Value Shell with Tests and CI

Data Structure DesignModel answer
MediumModel answerData Structure Designoopcliunit-testing

Last updated

Design a single-class interactive key-value shell that saves to disk, handles bad input, logs errors, accepts new commands easily, and ships with unit tests, a README, and CI.

Task

Build a small interactive terminal program, implemented as one class, that acts as a persistent key-value store. A user types commands at a prompt and the program replies.

Required commands

CommandBehavior
add <key> <value>Store a new entry
get <key>Print the value for a key
delete <key>Remove an entry
listShow every stored entry
helpDescribe the available commands
exitLeave the program

Requirements

  • Persistence: data is saved to a local file and reloaded on the next start.
  • Validation: bad input (unknown commands, wrong argument counts, malformed text) must never crash the program. The user gets a clear message instead.
  • Logging: errors are recorded through a logger.
  • Extensibility: adding a new command should not require rewriting the existing dispatch logic.
  • Tests: write thorough unit tests covering normal use, edge cases (duplicate keys, missing keys, malformed commands) and error handling.
  • Project hygiene: include a README with setup steps and how to run the tests, plus a GitHub Actions workflow that runs the tests on every push.

Discussion points

Be ready to explain:

  1. The class design: what state it holds, its methods, and where its dependency boundaries lie.
  2. How you would refactor it to support subcommands or separate modules.
  3. The trade-offs you made, assuming you only had about 60 minutes.

State your policy on ambiguous cases (for example, what happens when add is used on an existing key) before you code them.

Hints

Hint 1

Keep the interactive loop trivial. Put all logic in a method that takes one line of text and returns the output string, so tests never need a real terminal.

Hint 2

Store commands in a registry (a dict from name to handler plus argument count and help text). Then adding a command is one registration call and the dispatcher stays unchanged.

Hint 3

Inject the file path, input stream, output stream and logger through the constructor so tests can use temp directories and in-memory streams.

Hint 4

Write the state file to a temp file and rename it over the original so a crash cannot leave a half-written file; handle a corrupt or missing file on startup.