Text Editor with Undo

Asked at FigmaPhone screen

Reported by the problem's creator, not verified

You will build a class TextEditor that holds a piece of text, initially empty. Positions are zero-based indexes into the text.

An edit op is a list of one of these forms:

  • ["insert", index, text] inserts text at index
  • ["delete", start, end] removes the characters from start up to but not including end
  • ["replace", start, end, text] replaces that range with text

Each test drives one TextEditor instance through a sequence of calls.

Examples

  1. Example 1

    Input: init = [], operations = [["apply",[["insert",0,"hello"]]],["get_text",[]],["apply",[["insert",5," world"]]],["get_text",[]],["undo",[]],["get_text",[]],["apply",[["replace",0,5,"bye"]]],["get_text",[]],["undo",[]],["get_text",[]],["undo",[]],["get_text",[]],["undo",[]],["get_text",[]]]

    Output: [null,"hello",null,"hello world",null,"hello",null,"bye",null,"hello",null,"",null,""]

    Explanation: The first undo removes world. Undoing the replace restores all five letters of hello; the next undo removes it, and the last undo has nothing left to revert, so the text stays empty.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.