Code RoomDocument edit replay
EasyPrep Room Coding #4727

Document edit replay

CodingAlgorithms & data structuresEntry–Mid~15 min

A note taking app replays the command log of one editing session. The document starts empty. Each command is one of four strings. "type|hello" appends that text to the end of the document. "trim|3" removes the last three characters, and removes everything if fewer than three remain. "undo" restores the document to the state it had before the most recent applied edit. "redo" reapplies the most recently undone edit. A type or a trim counts as an edit even when it changes nothing, and every new edit discards whatever could still be redone. An undo with nothing to undo, or a redo with nothing to redo, is ignored. Return the document text after the whole log has been replayed.

Implement
replay_editor_session(commands: list[str]) → str
Examples
in[["type|hel","type|lo","undo"]]out"hel"
in[["type|abc","undo","type|xyz","redo"]]out"xyz"
in[["type|draft","trim|2","undo","undo","redo"]]out"draft"
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 15 min
InputExpectedGot
[["type|hel","type|lo","undo"]]"hel"not run yetsample
[["type|abc","undo","type|xyz","redo"]]"xyz"not run yetsample
[["type|draft","trim|2","undo","undo","redo"]]"draft"not run yetsample