Common Mistakes

What to avoid

  • Forgetting to sort first: Two pointers requires sorted input for the movement logic to work. Always check if input is sorted.
  • Infinite loops from wrong pointer movement: If both pointers move the same direction or neither moves, you'll loop forever.

Ensure exactly one pointer moves each iteration.

  • Off-by-one with while conditions: Use left < right not left <= right when pointers shouldn't meet.
  • Not handling duplicates in 3Sum: Skip identical values to avoid duplicate results.