FastPrepText Document History

Text Document History

Notion logoNotion● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations against an initially empty text document.

  • ["APPEND", text] appends text to the end.
  • ["DELETE", count] removes the final count characters.
  • ["UNDO"] restores the document state before the latest applied append or delete.
  • ["REDO"] reapplies the latest undone append or delete.
  • ["GET"] appends the current complete text to the result.

A new append or delete after an undo clears the redo history. An undo or redo with no available history has no effect. Return the values produced by GET operations in order.

Function

runTextDocument(operations: String[][]) → String[]

Examples

Example 1

operations = [["APPEND","hello"],["APPEND","!"],["DELETE","3"],["GET"],["UNDO"],["GET"],["REDO"],["GET"],["UNDO"],["APPEND","?"],["REDO"],["GET"]]return = ["hel","hello!","hel","hello!?"]

Undo and redo move between whole document states. Appending after the second undo clears the remaining redo state.

Constraints

  • 1 <= operations.length <= 20000.
  • Each operation has exactly one of the forms listed above.
  • Every appended text is a non-empty printable ASCII string.
  • Every delete count is a positive decimal integer no larger than the current document length.
  • The total length of all returned strings is at most 200000.

More Notion problems

See Notion hiring insights
public String[] runTextDocument(String[][] operations) {
    // Write your code here.
}
operations[["APPEND","hello"],["APPEND","!"],["DELETE","3"],["GET"],["UNDO"],["GET"],["REDO"],["GET"],["UNDO"],["APPEND","?"],["REDO"],["GET"]]
expected["hel", "hello!", "hel", "hello!?"]
Checking account…