Simple Database Engine - Problem

Build a simple key-value database engine that supports basic CRUD operations (Create, Read, Update, Delete) with efficient indexing and data persistence.

Your database engine should implement:

  • B-Tree indexing for fast key lookups
  • Disk persistence to save/load data
  • Basic operations: put(key, value), get(key), delete(key), list()

The engine should handle a sequence of operations and return the final state as a list of key-value pairs sorted by key.

Input & Output

Example 1 — Basic CRUD Operations
$ Input: operations = [["put","apple","red"],["put","banana","yellow"],["get","apple"],["delete","banana"],["put","cat","furry"]]
Output: [["apple","red"],["cat","furry"]]
💡 Note: Put apple→red, put banana→yellow, get apple returns red, delete banana, put cat→furry. Final state has apple and cat sorted by key.
Example 2 — Update Existing Key
$ Input: operations = [["put","key1","value1"],["put","key1","updated"],["get","key1"]]
Output: [["key1","updated"]]
💡 Note: Put key1→value1, then update key1→updated. Get returns updated value. Final state contains the updated pair.
Example 3 — Empty Result
$ Input: operations = [["put","temp","data"],["delete","temp"]]
Output: []
💡 Note: Put temp→data then delete temp. Database becomes empty, so return empty array.

Constraints

  • 1 ≤ operations.length ≤ 1000
  • operations[i].length is either 2 (for get/delete) or 3 (for put)
  • All keys and values are strings with length 1 ≤ length ≤ 100
  • Keys contain only lowercase letters and numbers

Visualization

Tap to expand
INPUTALGORITHMRESULTOperations Sequence["put", "apple", "red"]["put", "banana", "yellow"]["get", "apple"]["delete", "banana"]["put", "cat", "furry"]Hash Map Storage1Initialize empty hash map2Process put operations O(1)3Handle get/delete O(1)4Sort keys for output O(n log n)Hash Map State:apple → red, cat → furryFinal DatabaseSorted Key-Value Pairs["apple", "red"]["cat", "furry"]Array Format:[["apple","red"],["cat","furry"]]Key Insight:Hash maps provide O(1) operations but require final sorting, while B-trees maintain order naturally with O(log n) operations.TutorialsPoint - Simple Database Engine | Hash Map Implementation
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
23.2K Views
Medium Frequency
~45 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen