Document edit replay
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.
replay_editor_session(commands: list[str]) → str[["type|hel","type|lo","undo"]]out"hel"[["type|abc","undo","type|xyz","redo"]]out"xyz"[["type|draft","trim|2","undo","undo","redo"]]out"draft"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.
[["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