Problem · Design

Text Editor, Part 1: Basic Text Buffer

Learn this problem
MediumOpenAI logoOpenAIFULLTIMEPHONE SCREEN

Problem statement

Implement the text buffer for a text editor. The buffer begins empty and supports insertion, deletion, and reading the current text.

Supported operations

Each operation is a string array:

  • ["INSERT", position, text]: insert text immediately before the character at the zero-based position. A position equal to the current text length appends the text.
  • ["DELETE", start, end]: delete the characters in the half-open range [start, end).
  • ["GET"]: append the complete current text to the output.

Interview sequence

  1. Part 1: Basic Text Buffer
  2. Part 2: Undo and Redo

Function

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

Examples

Example 1

operations = [["INSERT", "0", "hello"], ["INSERT", "5", "!"], ["GET"], ["DELETE", "1", "4"], ["GET"]]return = ["hello!", "ho!"]

The first query reads hello!. Deleting indices 1 through 3 removes ell, leaving ho!.

More OpenAI problems

drafts saved locally
public String[] processTextBuffer(String[][] operations) {
  // write your code here
}
operations[["INSERT", "0", "hello"], ["INSERT", "5", "!"], ["GET"], ["DELETE", "1", "4"], ["GET"]]
expected["hello!", "ho!"]
checking account