FastPrepDocument Edit Events

Document Edit Events

ClickUp logoClickUp● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A document starts as the empty string. Process each event in order and return the final document.

  • ["INSERT", position, text] inserts text before the zero-based position.
  • ["DELETE", position, length] removes length characters beginning at position.

An event whose position or range is invalid for the current document is rejected and leaves the document unchanged.

Function

applyDocumentEdits(events: String[][]) → String

Examples

Example 1

events = [["INSERT","0","hello"],["INSERT","5"," world"],["DELETE","5","1"]]return = "helloworld"

The second event adds a leading space before world, and the delete removes that space.

Example 2

events = [["INSERT","0","abc"],["DELETE","4","1"],["INSERT","1","X"]]return = "aXbc"

The out-of-range delete is rejected; the final insert succeeds.

Constraints

  • 0 <= events.length <= 20000.
  • Inserted text and the final document together contain at most 200000 ASCII characters.
  • Numeric fields are nonnegative decimal integers.
See ClickUp hiring insights
public String applyDocumentEdits(String[][] events) {
    // Write your code here.
}
events[["INSERT","0","hello"],["INSERT","5"," world"],["DELETE","5","1"]]
expected"helloworld"
Checking account…