Implement command-driven in-memory key-value database
Problem: Implement a Command-Driven In-Memory Key-Value Database
Design and implement an in-memory key-value database that supports a specific set of commands. The database must handle up to 100,000 commands, maintain an average time complexity of O(1) per operation, and keep memory usage within reasonable bounds.
Keys and values are ASCII strings.
Supported Commands
- SET key value: Associates
keywithvalue. Ifkeyalready exists, its value is updated. - GET key: Retrieves the
valueassociated withkey. Prints thevalueorNULLif thekeydoes not exist. - DELETE key: Removes
keyand its associatedvaluefrom the database. Ifkeydoes not exist, no action is taken. - COUNT value: Prints the number of keys that currently map to the given
value. - BEGIN: Starts a new transaction. All subsequent
SETandDELETEoperations are part of this transaction. - ROLLBACK: Undoes all changes made in the most recent open transaction. If no transaction is currently open, it prints "NO TRANSACTION".
- COMMIT: Makes all changes from all currently open transactions permanent. If no transactions are open, no action is taken.
Requirements
- Design data structures to support the
COUNTcommand efficiently. - Implement transactional semantics using an undo log or an equivalent mechanism.
- Specify the precise I/O format for commands and outputs.
- Discuss edge cases, including:
- Nested transactions.
- Deleting non-existent keys.
- Multiple keys sharing the same value.
AI interview prep
Core dilemma
Balancing the requirement for O(1) average time complexity for basic key-value operations with the complexities introduced by transactional semantics (BEGIN, ROLLBACK, COMMIT) and the efficient `COUNT` operation, all while managing memory for transaction logs and handling nested transactions correctly.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement command-driven in-memory key-value database
Problem: Implement a Command-Driven In-Memory Key-Value Database
Design and implement an in-memory key-value database that supports a specific set of commands. The database must handle up to 100,000 commands, maintain an average time complexity of O(1) per operation, and keep memory usage within reasonable bounds.
Keys and values are ASCII strings.
Supported Commands
- SET key value: Associates
keywithvalue. Ifkeyalready exists, its value is updated. - GET key: Retrieves the
valueassociated withkey. Prints thevalueorNULLif thekeydoes not exist. - DELETE key: Removes
keyand its associatedvaluefrom the database. Ifkeydoes not exist, no action is taken. - COUNT value: Prints the number of keys that currently map to the given
value. - BEGIN: Starts a new transaction. All subsequent
SETandDELETEoperations are part of this transaction. - ROLLBACK: Undoes all changes made in the most recent open transaction. If no transaction is currently open, it prints "NO TRANSACTION".
- COMMIT: Makes all changes from all currently open transactions permanent. If no transactions are open, no action is taken.
Requirements
- Design data structures to support the
COUNTcommand efficiently. - Implement transactional semantics using an undo log or an equivalent mechanism.
- Specify the precise I/O format for commands and outputs.
- Discuss edge cases, including:
- Nested transactions.
- Deleting non-existent keys.
- Multiple keys sharing the same value.