Implement Undo/Redo for Block Edits
Problem
Implement undo/redo for a sequence of edit operations on a document (e.g. typing, deleting, or formatting a block). undo() reverts the most recent operation; redo() re-applies the most recently undone operation. A new edit after some undos discards the redo history.
Input / Output
- Input: a stream of edit operations, interleaved with
undo()/redo()calls. - Output: the document state (or the operation applied/reverted) after each call.
Constraints
- History is bounded — cap the number of retained operations and drop the oldest when full.
undo()on empty history andredo()with nothing to redo are no-ops.- Each edit must carry enough information to be reversed.
Example
type "a"; type "b" -> "ab"
undo() -> "a"
undo() -> ""
redo() -> "a"
type "c" -> "ac" // redo of "b" is now discarded
added …