FunctionRacerProblemsRace

Text Buffer With Undo and Redo

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class TextBuffer that holds a piece of text and can undo and redo its edits. There are two kinds of edit: appending to the end and deleting from the end. Write these methods:

Making a new edit throws away every edit that could still be redone. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

TextBuffer()
append("ab") returns 2
append("cd") returns 4
delete_last(3) returns "bcd"
undo_edit() returns "abcd"
undo_edit() returns "ab"
redo_edit() returns "abcd"
append("x") returns 5
redo_edit() returns "abcdx"
undo_edit() returns "abcd"

The delete removes bcd. Two undos go back to abcd and then ab, and the redo brings abcd back. Appending x is a new edit, so the undone delete can no longer be redone; the last undo removes the x.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems