<aside>

This section is part of the 5-hour LeetCode DSA Foundations course by Mukul Raina

C11: LeetCode DSA Foundations for Interviews

Section 6: Two Pointers


SLIDE 51: Section Intro, Two Pointers

Section 6: Two Pointers

Two pointers is a technique where two index variables traverse a data structure in a coordinated way, eliminating the need for nested loops. Instead of checking every possible pair with O(n^2) brute force, two pointers exploits structure in the input (usually sorted order) to find the answer in O(n).

The core idea: if the input has some ordering property, you do not need to check every combination. You can make an informed decision about which pointer to move, discarding large portions of the search space with each step.

What We’ll Cover:

  1. The two-pointer mental model and why it converts O(n^2) to O(n)
  2. Opposite-direction pointers (left/right moving inward)
  3. Same-direction pointers (read/write for in-place operations)
  4. Decision framework: two pointers vs hash map
  5. Worked example: Three Sum with duplicate handling
  6. How to present two-pointer solutions in interviews

Where This Fits in the Course:

Two pointers extends the array techniques from Section 3 and often serves as an alternative to the hash map approach from Section 5. The difference: hash maps trade O(n) space for O(1) lookups on unsorted input, while two pointers achieves O(n) time with O(1) space on sorted input. Sliding window (Section 7) is a specialized form of same-direction two pointers applied to subarray optimization problems. Binary search (Section 8) also operates on sorted input but solves a different class of problems (finding a specific element or boundary rather than finding pairs).

<aside>

Mukul Raina | youtube.com/@mukulraina | linkedin.com/in/mukulraina

</aside>