← Questions

Live string editing with a mouse cursor

0
HarveyCodingLast asked Apr 2026

A coding interview question asked in Harvey's coding round, last asked Apr 2026. It tests strings, text editing and cursor.

Implement the core of a text box being edited live: a string, a cursor that the user places with the mouse or moves with the arrow keys, and typing and deleting at the cursor. A follow-up adds selecting a range with the mouse.

Part 1: cursor and edits

  • type(text: str) -> None inserts text at the cursor; the cursor ends after the inserted text.
  • click(pos: int) -> None places the cursor before character pos (0 is the start); out-of-range positions are clamped to [0, len].
  • move_left(k: int) -> None, move_right(k: int) -> None move the cursor by up to k, stopping at the ends.
  • backspace(k: int = 1) -> None deletes up to k characters before the cursor; delete(k: int = 1) -> None deletes up to k after it.
  • text() -> str and cursor() -> int report the state.

Part 2: mouse selection

  • select(start: int, end: int) -> None selects the characters between two positions, in either order (a drag from right to left is allowed); the cursor goes to the larger end. An empty range means no selection.
  • While a selection exists, type replaces it, and backspace or delete removes it (whatever k is). Any click or arrow move clears it.

Interface

Unless you agree on a different interface with the interviewer, assume the methods above on a class Editor() that starts empty.

Example

type("hello world")    text "hello world"   cursor 11
click(5); type(",")    text "hello, world"  cursor 6
move_right(100)        cursor 12            # clamped to the end
backspace(7)           text "hello"         cursor 5
click(0); type(">> ")  text ">> hello"      cursor 3
select(8, 3)           # selects "hello", cursor 8
type("bye")            text ">> bye"        cursor 6
move_left(2); delete(1)  text ">> be"       cursor 4
select(0, 3); backspace()  text "be"        cursor 0

Constraints

  • Up to 10^5 operations on a text of up to 10^6 characters.
  • Assume plain characters (no multi-character graphemes) unless the interviewer brings them up.

Clarifying questions

  • What happens when the user clicks past the end of the text?
  • Does the cursor go to the start or end of a selection, and does the drag direction matter?
  • Does typing over a selection replace it, and does Backspace delete the selection or one more character too?
  • How large can the text get, and which operations are most frequent?

What a strong answer covers

  • A correct simple version first (a string or list plus a cursor index), with clamping on every move.
  • A clear model for the selection and every operation's effect on it.
  • For large texts: a gap buffer (two stacks around the cursor) makes typing and deleting at the cursor O(1) amortized, with clicks costing O(distance moved); a rope or piece table for many far-apart edits.
  • Tests for edges: empty text, deleting past the start or end, zero-length selection, reversed selection.

Follow-ups

  • Add undo and redo.
  • Support several cursors at once, as code editors do.
  • Add word-wise movement (Ctrl+Left / Ctrl+Right) and double-click to select a word.
  • Two users edit the same text at once. What breaks, and how would you merge their edits?

Where it's asked

  • Harvey · Coding · Last asked Apr 202660 min · Live practical coding · Candidates report freedom to use the tools and languages they know; confirm with your recruiter

What it tests

strings · text editing · cursor

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding