Undo and Redo Command History
Reported by candidates from Netflix's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Netflix reported this one in August 2026, and it looks too easy to be dangerous. Undo and Redo Command History is a two-stack simulation. The edge case that breaks the naive version is the one where EXECUTE has to wipe the redo stack. Miss that and Example 2 fails on the REDO after "d". If you've got an OA invite and 48 hours, this is a pattern you can lock in tonight. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the stack logic in real time.
The problem
Maintain an active command history while processing a sequence of operations. Each row in operations has one of these forms: ["EXECUTE", command]: append the nonempty, case-sensitive command identifier to the active history. Executing a new command clears every command currently available for redo. ["UNDO"]: remove the most recently active command and make it available for redo. If the active history is empty, do nothing. ["REDO"]: restore the most recently undone command to the end of the active history. If no command is available for redo, do nothing. After every operation, encode the complete active history as one string. Join its command identifiers in application order with |. Encode an empty active history as the empty string. Return the encoded snapshots in operation order. Function runUndoRedoHistory(operations: String[][]) → String[] Examples Example 1 operations = [["EXECUTE","build"],["EXECUTE","test"],["UNDO"],["REDO"]] return = ["build","build|test","build","build|test"] The undo removes test from the active history, and redo restores that same command. Example 2 operations = [["EXECUTE","a"],["EXECUTE","b"],["EXECUTE","c"],["UNDO"],["UNDO"],["EXECUTE","d"],["REDO"],["UNDO"]] return = ["a","a|b","a|b|c","a|b","a","a|d","a|d","a"] After two undos, executing d clears the redo branch. The following redo is therefore a no-op, and the final undo removes d. Example 3 operations = [["UNDO"],["REDO"],["EXECUTE","x"],["UNDO"],["UNDO"],["REDO"],["REDO"]] return = ["","","x","","","x","x"] Undo and redo are no-ops when their corresponding history is empty. The command x can be restored only once. Constraints 1 <= operations.length <= 500 Each row is exactly ["UNDO"], ["REDO"], or ["EXECUTE", command]. 1 <= command.length <= 100. Each command identifier contains only uppercase or lowercase ASCII letters, digits, underscores, or hyphens and is compared by exact, case-sensitive equality. The total number of command identifiers across all returned snapshots is at most 125000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep two stacks: active and redo. EXECUTE pushes onto active and clears redo. UNDO pops active, if nonempty, and pushes onto redo. REDO pops redo, if nonempty, and pushes onto active. After every operation, join active with "|" and append that string to the result. The pitfall is clearing redo on EXECUTE. The second pitfall is no-ops: UNDO on empty history and REDO on empty redo must still emit a snapshot, even if it's the same as before or empty. Don't skip the output. Cost is fine. With at most 500 operations and 125000 total identifiers, rebuilding the string each time is acceptable, so don't over-engineer it. If the stack handling slips under pressure, StealthCoder is the hedge during the live OA. It reads the prompt and gives you a working solution while you stay in the editor.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Undo and Redo Command History cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Netflix's OA.
Netflix reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Undo and Redo Command History FAQ
How hard is Undo and Redo Command History really?+
Easy to medium. There's no clever algorithm, just careful state handling. The difficulty is remembering that EXECUTE clears redo and that no-op operations still produce a snapshot. If you trace Example 2 by hand once, you'll catch both.
What's the trick to solving it?+
Use two stacks, one for active history and one for redo. EXECUTE pushes to active and empties redo. UNDO moves the top of active to redo. REDO moves the top of redo back to active. Snapshot after every operation.
What edge cases should I test before submitting?+
Test UNDO and REDO on empty state, like Example 3. Test EXECUTE after an UNDO, so redo gets cleared. Test a command restored only once. Also confirm an empty history outputs an empty string, not null or a missing entry.
Do I need to optimize the snapshot building?+
No. Operations max out at 500 and the total identifiers across snapshots is capped at 125000. Joining the active stack with "|" after each operation is fast enough. Spend your effort on correctness instead.
How do I prepare for this in 48 hours?+
Write the two-stack solution from scratch once, then run all three examples by hand. Practice similar state-machine simulations like browser history or text editor undo. Focus on clearing and no-op rules, since that's where wrong answers come from.