Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Same-direction Pointers invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Advance read and write pointers in one direction for compaction, partitioning, or deduplication. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Same-direction Pointers when the prompt's constraints and required operations match this shape: Advance read and write pointers in one direction for compaction, partitioning, or deduplication.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A read pointer inspects every input item; a write pointer marks the next position in the valid compacted output. They move in the same direction but answer different questions.
Before processing read, the half-open prefix [0, write) contains exactly the accepted items from earlier input, in order. Writing only at write preserves values that the reader has not consumed.
def compact_trace(values):
trace = []
write = 0
for read, value in enumerate(values):
if read == 0 or value != values[read - 1]:
write += 1
trace.append([read, write, value])
return traceImplement compact_trace(values). Return [read, write, value] after processing each value while compacting adjacent duplicates in sorted input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Read advances every iteration. Write advances only when the current value belongs in the result. This gives O(n) time and O(1) auxiliary space, while the suffix beyond the returned logical length is unspecified.
def compact_unique(values):
write = 0
for value in values:
if write == 0 or values[write - 1] != value:
values[write] = value
write += 1
return writeImplement compact_unique(values). Compact sorted values in place and return the new logical length.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use in-place read/write when mutation is allowed and stable compaction matters. Build a new list when preserving the caller’s container or maximizing clarity matters more than O(1) extra space.
Say: “Everything before write is the finalized compacted prefix; read examines the next source value. On acceptance I write then advance.” Clarify the returned logical length and whether elements after it matter.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Same-direction Pointers invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Same-direction Pointers decision loop | O(n) | O(n) | Scan left to right with a read index while the end of a separate result acts as the write frontier. |
O(n) for the focused Compact Sorted Values implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Compact Sorted Values before optimizing.
Avoid
def compact_sorted_values(values):
passUse instead
def compact_sorted_values(values):
if not values:
return []
compact = [values[0]]
for read in range(1, len(values)):
if values[read] != compact[-1]:
compact.append(values[read])
return compactWhere you will hit this: Compact Sorted Values(opens in a new tab)
Appends every scanned value and therefore preserves duplicates.
Prevent it: Use the public tests and preserve this state: The prefix before write contains exactly the accepted items.
Avoid
def compact_sorted_values(values):
return list(values)Use instead
def compact_sorted_values(values):
if not values:
return []
compact = [values[0]]
for read in range(1, len(values)):
if values[read] != compact[-1]:
compact.append(values[read])
return compactWhere you will hit this: Compact Sorted Values(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n).
Avoid
def compact_sorted_values(values):
return list(values)Use instead
def compact_sorted_values(values):
if not values:
return []
compact = [values[0]]
for read in range(1, len(values)):
if values[read] != compact[-1]:
compact.append(values[read])
return compactWhere you will hit this: Add Two Numbers(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
hello-interview · checked 2026-07-12