KatInterview

Simulate a Cursor Editor with Sticky-Column Vertical Moves

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designsimulationdesigncursor

Last updated

Replay a log of editor commands (type, backspace, move in four directions, render) on a single-cursor multi-line buffer, with a column that persists across vertical moves.

You are given a recorded session for a small text editor. Replay it and report what the editor shows whenever it is asked to render.

The editor

The editor holds a sequence of text units and one cursor. The cursor sits in a gap between units, so it can take any position from 0 (before everything) to the number of units (after everything). The editor starts empty with the cursor at 0. There is no selection.

Input

  • operations: a list of command names.
  • arguments: a list of the same length. arguments[i] is the list of arguments for operations[i].

Each command is one of the following.

CommandArgumentsEffect
"init"noneReset to an empty editor with the cursor at 0.
"type_in"[unit]Insert unit as one unit immediately before the cursor. The cursor then sits right after it.
"backspace"noneRemove the unit immediately before the cursor, and the cursor moves back by one. If the cursor is at position 0, nothing changes.
"move_cursor"[direction]Move the cursor. direction is "left", "right", "up" or "down".
"str"noneRender the text with a `

Lines

A unit that is exactly the single newline character ("\n") is a line break. Every other unit is ordinary text, even if it looks like an escape sequence. Lines are the pieces of text separated by line breaks. The line break belongs to the end of its line, and the cursor directly after a line break is at column 0 of the next line. A line's length is the number of units in it, not counting its line break.

The column of the cursor is its distance from the start of the line it is on.

Movement rules

  • "left": move one unit left. At position 0 nothing happens. Moving left from the start of a line crosses the line break to the end of the previous line.
  • "right": move one unit right. At the very end nothing happens. Moving right at the end of a line crosses the line break to the start of the next line.
  • "up" / "down": move to the previous or next line. If there is no such line, the cursor stays where it is. Otherwise the cursor lands on the desired column of the target line, or at the end of the target line if the line is shorter than that.

Desired column

The desired column makes vertical movement remember where you started:

  • When an "up" or "down" command runs and no desired column is currently remembered, the desired column becomes the cursor's current column. This happens even if the move itself has no effect because there is no target line.
  • While a desired column is remembered, consecutive "up" / "down" commands keep using it. The cursor may be clamped to a short line, but the desired column does not shrink.
  • Any "type_in", "backspace", "left", "right" or "init" command forgets the desired column. A "str" command does not.

Output

Return a list with one entry per command. The entry is the rendered string for "str" commands and None for all other commands. The rendered string is the units before the cursor, then |, then the units after the cursor, with line breaks left as they are.

Constraints

  • operations and arguments have equal length, at most a few thousand entries.
  • Every command name and direction is one of the values listed above.
  • "type_in" always has exactly one argument. "move_cursor" always has exactly one argument.
  • The input contains no unknown commands.

Examples

Example 1

Input: run_editor_session(['init', 'type_in', 'type_in', 'type_in', 'str', 'backspace', 'str'], [[], ['a'], ['b'], ['c'], [], [], []])

Output: [None, None, None, None, 'abc|', None, 'ab|']

Example 2

Input: run_editor_session(['init', 'type_in', 'type_in', 'move_cursor', 'str', 'type_in', 'str', 'move_cursor', 'str'], [[], ['a'], ['b'], ['left'], [], ['x'], [], ['right'], []])

Output: [None, None, None, None, 'a|b', None, 'ax|b', None, 'axb|']

Example 3

Input: run_editor_session(['init', 'type_in', 'type_in', 'type_in', 'type_in', 'type_in', 'move_cursor', 'str'], [[], ['a'], ['b'], ['\\n'], ['c'], ['d'], ['up'], []])

Output: [None, None, None, None, None, None, None, 'ab\\ncd|']

Hints

Hint 1

Store the text as a list of units plus an integer cursor index. Typing is an insert at the index, backspace is a delete at index - 1, and rendering joins the two halves around a '|'.

Hint 2

For a vertical move, scan the text once to record where every line starts (index 0, and the index after each newline). Find the last start that is <= cursor to get the current line and column.

Hint 3

Keep a separate 'desired column' variable. Set it on the first up/down of a streak if it is empty, clear it on every other editing or horizontal command, and clamp to the target line's length when landing.