Editable sequence with a marker
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Feb 2026. It tests object-oriented design and linked lists.
Design a mutable ordered sequence of elements with a marker that points to one element. Support inserting and removing a contiguous block of elements at a position, moving a block from one position to another, and setting the marker. The marker must stay on the right element through every edit. Explain the API, the data model, how the marker behaves when edits happen before it, after it or on it, and the time-complexity trade-offs of your implementation choices.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
insert(pos: int, items: list) -> None— insertitemsso the first of them ends up at indexpos. -
remove(pos: int, count: int) -> None— removecountelements starting atpos. -
move(src: int, count: int, dst: int) -> None— take the block[src, src + count)out, then insert it at indexdstof the remaining sequence. -
set_marker(pos: int) -> None;marker() -> int | None(the marked index);marked() -> element | None.
Marker rules to agree on (a reasonable default):
- An edit entirely before the marker shifts its index; an edit after it leaves it alone.
- If the marked element moves, the marker moves with it.
- If the marked element is removed, the marker goes to the first element after the removed block, else the one before it, else
None.
Example
s = Sequence(list("abcdef")); s.set_marker(3) # on "d"
s.insert(1, ["x", "y"]) -> a x y b c d e f, marker 5, marked "d"
s.move(4, 3, 0) -> c d e a x y b f, marker 1, marked "d" # block "cde" moved to the front
s.remove(0, 2) -> e a x y b f, marker 0, marked "e" # "d" removed; next element is "e"
Clarifying questions
- What should happen to the marker when its element is deleted?
- Is
dstinmovean index before or after the block is taken out? - How long is the sequence, and which operations are most frequent?
- Do we need several markers, or undo?
What a strong answer covers
- Validating positions and counts, and defining
moveprecisely (including a move to a place inside the block itself). - Three implementations: a dynamic array (simple,
O(n)per edit, marker kept as an index and adjusted); a doubly linked list with the marker as a node reference (block editsO(1)once you hold the nodes, but finding a position isO(n)); a balanced tree or rope with subtree sizes (all operationsO(log n), marker as a node handle whose index is computed on demand). - Why storing the marker as a reference to the element instead of an index removes most special cases.
- Tests for every marker rule: edits before, after, containing the marker; moving the marked block; removing everything.
Follow-ups
- Support many markers (cursors of several users), as in a collaborative editor.
- Add undo and redo.
- The sequence holds 10^7 elements and edits are frequent.
Where it's asked
- Google · Onsite coding · Last asked Feb 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
object-oriented design · linked lists
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
