FastPrepExecute Cursor-Based Text Editor Operations

Execute Cursor-Based Text Editor Operations

DocuSign logoDocuSign● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Start with an empty document and cursor position 0. Each command is [operation,argument].

  • INSERT text inserts at the cursor and moves past it.
  • LEFT x and RIGHT x move without crossing an edge.
  • BACKSPACE x removes up to x characters immediately left of the cursor and moves to the deletion start.
  • DELETE x removes up to x characters immediately right of the cursor without moving it.
  • PRINT x records characters at indices from cursor-x through cursor+x, inclusive and clipped to the document.

Return recorded strings in print order.

Function

getPrintedStrings(commands: String[][]) → String[]

Examples

Example 1

commands = [["Insert","addthis"],["Print","5"],["Left","4"],["Right","2"],["Backspace","1"],["Delete","1"],["Print","10"]]return = ["dthis","addts"]

The print results are dthis and addts.

Example 2

commands = [["Insert","abc"],["Left","2"],["Insert","XY"],["Print","3"]]return = ["aXYbc"]

Insertion at cursor one creates aXYbc and the cursor sits after XY.

Example 3

commands = [["Print","2"]]return = [""]

Printing an empty document records an empty string.

Constraints

  • 1 <= commands.length <= 5000.
  • The total inserted length is at most 5000.
  • Numeric arguments are nonnegative decimal integers.

More DocuSign problems

See DocuSign hiring insights
public String[] getPrintedStrings(String[][] commands) {
    // write your code here
}
commands[["Insert","addthis"],["Print","5"],["Left","4"],["Right","2"],["Backspace","1"],["Delete","1"],["Print","10"]]
expected["dthis", "addts"]
Checking account…