Check Why Each Move Was Safe

Every exercise in this section moved two positions through your data, and each move followed a rule. That rule is what makes the approach correct: a move may only discard candidates that can’t be part of the answer. In return, you usually get O(1) auxiliary space, as long as a precondition holds, such as sorted input or permission to modify the list.

Here’s how the moves worked in each exercise:

  • Mirror Text: Comparing characters from both ends inward avoids the O(n) copy that a reversed slice creates.
  • Merge Ordered Readings: One position per input, taking the smaller front value each time, keeps the output sorted.
  • Compact Repeated Values: A read position scans ahead while a write position marks the end of the unique prefix. Sorted input keeps duplicates right next to each other.
  • Move Empty Slots: A write position that trails the reader keeps nonzero values in their original order inside the same list object.
  • Ordered Target Pair: When a sum is too large, the right value can’t pair with anything left, so moving that pointer is safe. Sorted order took over the job of the dictionary from Target Pair Positions.
  • Nearly Mirrored Text: At the first mismatch, either character could be the extra one, so you had to check both remaining ranges.

When you explain a two-pointer solution, state the precondition you relied on and say why the candidate you skipped couldn’t have worked. That one sentence is often what an interviewer is waiting to hear.

Some of these probably clicked right away, while others took a few rounds of failing tests. That’s normal, and those rounds are worth a second look! A failed test, an opened hint, or a first draft that copied the whole string tells you which part of the idea is still settling in. Pick the exercise that took you the most attempts, and in two or three sentences, explain what first went wrong and what changed your mind.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.