Code RoomLSM-tree memtable
HardPrep Room Coding #1533

LSM-tree memtable

CodingStorage & CDNAlgorithms & data structuresSenior–Staff~32 min

Simulate an LSM-tree memtable with flush-on-threshold. Writes go to an in-memory memtable (a key→value map). When the memtable's distinct-key count reaches `threshold` immediately after a write, it is flushed: its sorted contents become a new immutable SSTable (level-0, newest first) and the memtable is cleared. A read for a key returns the memtable's value if present, otherwise scans SSTables from newest to oldest and returns the first hit, otherwise -1. Process operations ["put", key, value] and ["get", key]. Return [results, sstable_count] where results is the list of get results in order and sstable_count is the number of SSTables produced by flushes. Keys are strings, values are non-negative integers; threshold is a positive integer.

Implement
lsm_memtable(threshold: int, ops: list[list]) → list
Examples
in[2,[["put","a",1],["put","b",2],["get","a"],["put","a",9],["get","a"]]]out[[1,9],1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 32 min
InputExpectedGot
[2,[["put","a",1],["put","b",2],["get","a"],["put","a",9],["get","a"]]][[1,9],1]not run yetsample