Skip to main content

Two Pointers Pattern: LeetCode Guide & Problems

Use two indices moving toward each other or in the same direction to solve linear problems.

✓ Pattern Solutions & Guides Practice Mode Enabled
51 Problem 7 Easy 43 Medium 1 Hard

Problems (51)

Problem Solution Link
Two Sum II - Input Array Is Sorted
#Array #Two Pointers #Binary Search
Solution
3Sum
#Array #Two Pointers #Sorting
Solution
Container With Most Water
#Array #Two Pointers #Greedy
Solution
Trapping Rain Water
#Array #Two Pointers #Stack
Solution
Valid Palindrome
#String #Two Pointers
Solution
Move Zeroes
#Array #Two Pointers
Solution
Remove Duplicates from Sorted Array
#Array #Two Pointers
Solution
4Sum
#Array #Sorting #Two Pointers
Solution
Sort Colors
#Array #Sorting #Two Pointers
Solution
Squares of a Sorted Array
#Array #Two Pointers #Sorting
Solution
Boats to Save People
#Array #Two Pointers #Greedy
Solution
Valid Palindrome II
#String #Greedy #Two Pointers
Solution
Minimum Length of String After Deleting Similar Ends
#String #Two Pointers
Solution
Encode and Decode Strings ★
#Array #String #Design
Solution
Best Time to Buy and Sell the Stocks - 2
#Array
Solution
3Sum Closest
#Array
Solution
Intersection of two Arrays
#Array
Solution
Maximum Sum Rectangle
#Array
Solution
Rotate Array
#Array
Solution
Majority Element
#Array
Solution
Majority Element II
#Array
Solution
Next Greater Element III
#Array
Solution
Merge Sort
#Array
Solution
Count Inversions
#Array
Solution
First Missing Positive
#Array
Solution
Repeat and Missing Number Array
#Array
Solution
Minimum Platforms
#Array
Solution
Max sum path in two arrays
#Array
Solution
Pairs of Songs With Total Durations Divisible by 60
#Array
Solution
Rectangle Overlap
#Array
Solution
Find the Duplicate Number
#Array
Solution
Segregate Even and Odd numbers
#Array
Solution
Subarray with 0 sum exist
#Array
Solution
Count Numbers with Unique Digits
#Array
Solution
Distribute Candies
#Array
Solution
Transpose Matrix
#Array
Solution
Pascals Triangle
#Array
Solution
Insert Delete GetRandom in O
#Array
Solution
Sign of the Product of an Array
#Array
Solution
Find N Unique Integers Sum up to Zero
#Array
Solution
Shuffle an Array
#Array
Solution
Count Primes
#Array
Solution
Power of Three
#Array
Solution
Hamming Distance
#Array
Solution
Excel Sheet Column Number
#Array
Solution
Excel Sheet Column Title
#Array
Solution
Happy Number
#Array
Solution
Assign Cookies
#Array
Solution
Reverse words in a given string
#String
Solution
Reverse Words in a String
#String
Solution
Minimum Number of Moves to Make Palindrome
#String
Solution
🧠

Mastering the Two Pointers Pattern for Coding Interviews

1. Mental Model & Visual Intuition

The Two Pointers pattern is designed to eliminate redundant work and reduce search spaces. Rather than evaluating every possible state using a naive brute-force approach, this pattern relies on an invariant that allows you to safely discard subproblems that cannot lead to an optimal answer. When encountering an interview question, look for core triggers such as monotonicity, contiguous constraints, optimal substructure, or overlapping subproblems.

2. Algorithmic Invariants & Step-by-Step Execution

To implement this pattern flawlessly under interview conditions, structure your thought process into three discrete steps:

  • State Initialization: Define your boundaries, pointers, or memoization tables clearly before entering loops.
  • State Transition: Update tracking variables incrementally as new elements enter or leave your active evaluation window.
  • Feasibility Check: When constraints are violated, systematically shrink or adjust your state until the valid invariant is restored.

3. Time and Space Complexity Trade-Offs

In top-tier technical interviews at Google, Meta, and Amazon, arriving at a working solution is only half the battle. You must explicitly justify why your approach achieves optimal Big-O bounds. For Two Pointers, the target time complexity is typically O(n) — both pointers traverse at most n elements total while maintaining an auxiliary space complexity of O(1) — no extra space needed. Always discuss whether an in-place modification or two-pass traversal can save memory.

4. Common Interview Edge Cases to Test

Before informing your interviewer that you have completed coding, trace your logic against these standard high-failure test cases:

  • Empty inputs, single-element collections, or all-identical values.
  • Inputs with negative numbers, duplicates, or extreme values that could cause integer overflow.
  • Odd versus even length inputs when dealing with pointer convergence or tree midpoints.

Related Guide

Two Pointers: The Complete Guide →

Converging, same-direction, fast & slow — the two-pointer family is bigger than most engineers reali…

Buy me a coffee
Daily Practice
0/0