Maintain Editable Text around a Bounded Cursor

Quick Overview

Implement a text editor supporting insertion, backspace and bounded cursor movement, with a ten-character preview before the cursor.

Maintain Editable Text around a Bounded Cursor

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Implement `TextEditor`, initially containing empty text with its cursor at the beginning. - `addText(text)` inserts the supplied text at the cursor and leaves the cursor immediately after it. - `deleteText(k)` removes up to `k` characters immediately to the left of the cursor and returns the number actually removed. - `cursorLeft(k)` and `cursorRight(k)` move up to `k` positions in the named direction without crossing either end of the text. Each returns the last `min(10, len)` characters to the left of the new cursor, where `len` is the number of characters on that side. ```cpp class TextEditor { public: TextEditor(); void addText(string text); int deleteText(int k); string cursorLeft(int k); string cursorRight(int k); }; ``` ### Examples **Example 1:** ```text Calls: TextEditor(); addText("abcd"); cursorLeft(2); addText("xy"); cursorRight(1); deleteText(3); cursorLeft(8) Returned values: [null, null,"ab", null,"abxyc",3,""] Explanation: The cursor states are "abcd|", "ab|cd", "abxy|cd", "abxyc|d", "ab|d", and "|abd". Deletion removes three characters; the final cursor has no text to its left. ``` **Example 2:** ```text Calls: TextEditor(); addText("wxyz"); deleteText(9); cursorRight(2) Returned values: [null, null,4,""] Explanation: Deleting at most nine characters removes all four available characters. Moving right in the empty editor returns an empty preview. ``` ### Constraints - `1 <= text.length, k <= 40` for method arguments. - Inserted text contains lowercase English letters. - At most `20000` total calls to the four editing and movement methods. **Follow-up:** Can each call take `O(k)` time, where insertion processes the supplied text length? ### Clarifying Questions - Is deletion the same as backspace? Yes; it only removes characters to the cursor's left. - Does moving the cursor delete text? No. - What if a requested move or deletion crosses an end? Stop at the boundary. - What does a movement call return? Up to ten characters immediately before the cursor, in their normal reading order. ```hint Check the boundary Trace the cursor separately from the text when a move reaches either end. ```

Overview: Implement a text editor supporting insertion, backspace and bounded cursor movement, with a ten-character preview before the cursor.

Squarepoint logo
Squarepoint
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement TextEditor, initially containing empty text with its cursor at the beginning.

  • addText(text) inserts the supplied text at the cursor and leaves the cursor immediately after it.
  • deleteText(k) removes up to k characters immediately to the left of the cursor and returns the number actually removed.
  • cursorLeft(k) and cursorRight(k) move up to k positions in the named direction without crossing either end of the text. Each returns the last min(10, len) characters to the left of the new cursor, where len is the number of characters on that side.
class TextEditor {
public:
    TextEditor();
    void addText(string text);
    int deleteText(int k);
    string cursorLeft(int k);
    string cursorRight(int k);
};

Examples

Example 1:

Calls: TextEditor(); addText("abcd"); cursorLeft(2); addText("xy"); cursorRight(1); deleteText(3); cursorLeft(8)
Returned values: [null, null,"ab", null,"abxyc",3,""]
Explanation: The cursor states are "abcd|", "ab|cd", "abxy|cd", "abxyc|d", "ab|d", and "|abd". Deletion removes three characters; the final cursor has no text to its left.

Example 2:

Calls: TextEditor(); addText("wxyz"); deleteText(9); cursorRight(2)
Returned values: [null, null,4,""]
Explanation: Deleting at most nine characters removes all four available characters. Moving right in the empty editor returns an empty preview.

Constraints

  • 1 <= text.length, k <= 40 for method arguments.
  • Inserted text contains lowercase English letters.
  • At most 20000 total calls to the four editing and movement methods.

Follow-up: Can each call take O(k) time, where insertion processes the supplied text length?

Clarifying Questions Guidance

  • Is deletion the same as backspace? Yes; it only removes characters to the cursor's left.
  • Does moving the cursor delete text? No.
  • What if a requested move or deletion crosses an end? Stop at the boundary.
  • What does a movement call return? Up to ten characters immediately before the cursor, in their normal reading order.