Undo and Redo Stack for a Sketch Editor
Last updated: November 29, 2026
Quick Overview
Implement a command history for a simple 2D sketch editor that supports execute, undo, and redo, where executing a new command after an undo discards the redo history and the history is capped at a fixed number of steps.
Autodesk
November 29, 20260
0
0 solved
Implement a command history for a simple 2D sketch editor that supports execute, undo, and redo, where executing a new command after an undo discards the redo history and the history is capped at a fixed number of steps.
Every Autodesk desktop product (AutoCAD, Fusion, Revit) is built around undoable commands, so this tests whether a candidate reaches for the command pattern instead of snapshotting the whole document. Interviewers look for correct redo invalidation and a clean object model.
What the Interviewer Expects
- Use the command pattern with do and undo on each command
- Two stacks, and clear the redo stack on a new execute
- Cap history and evict the oldest command
- Return a signal when there is nothing to undo or redo
- Explain why inverse operations beat full snapshots for large models
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you group several commands (a drag that fires many moves) into one undo step?
- What if a command fails halfway through its do step?
- How does undo work when two users edit the same cloud design?
- When would you store snapshots or diffs instead of inverse operations?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: execute(cmd), undo(), redo(). Each command knows how to apply and reverse itself against the document. A new execute after an undo kills the redo branch (linear history, like AutoCAD...
High-Level Design
Two stacks: undo_stack holds applied commands, redo_stack holds undone ones. Undo pops from one, calls undo, pushes onto the other; redo is the mirror. Commands store only what they need to reve...