The memory constraint is where I almost tripped up.
Start by clarifying the semantics: batch() marks a checkpoint, and undo() reverts all changes since that checkpoint. Use a map to store original values for keys modified after batch(), but only record a key's value the first time it's changed in the batch. On undo(), iterate through the map and restore each key to its original value, then clear the map.
Pro tip: Mention that you'd use a sentinel value (e.g., a unique object) to represent 'key did not exist before batch' so you can correctly delete keys that were newly added during the batch, rather than setting them to undefined.
Ask about nested batches, undo without batch, and whether undo can be called multiple times. Confirm that only the first modification per key should be recorded.
Use a Map (or object) to store original values for keys modified since the last batch. Include a flag or sentinel to track whether a key existed before the batch.
batch() initializes/resets the tracking map. In apply(), before updating a key, check if it's already in the map; if not, record its current value (or sentinel if absent).
Iterate over the tracking map: for each key, restore its original value (or delete it if sentinel). Then clear the map to allow a new batch.
Time: O(1) for apply and batch, O(k) for undo where k is number of modified keys. Space: O(k). Discuss alternative approaches like logging all operations vs. storing only first values.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.