Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Two Pointers

Two indices moving through the data so each step rules work out instead of repeating it.

Beginner9 problems4 interactive subtopicsO(n) time · O(1) space
Practice in AI DSA Open full visualizer
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (4)Open in full playground →
STEP 1/5Pointers at both ends
target = 8
01234567
−4
−1
2
3
5
7
10
13
left
right
  • inside the window
  • active this step
  • ruled out
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [-4, -1, 2, 3, 5, 7, 10, 13], target = 8
1def two_sum_sorted(nums, target):
▶2 left, right = 0, len(nums) - 1
3 while left < right:
4 total = nums[left] + nums[right]
5 if total == target:
6 return [left, right]
7 elif total < target:
8 left += 1
9 else:
10 right -= 1
11 return []
STEP 1 OF 5Setup

Pointers at both ends

left points at −4, the smallest value; right points at 13, the largest.

VARIABLES

left
0
right
7
total
-
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Two indices walk an array or string - toward each other or in the same direction. A comparison decides which one moves.

Why it matters

It replaces an O(n²) pair scan with one O(n) pass when the data has order you can use.

Think of it like this

Two friends reading a sorted price list from opposite ends to find two items that exactly fit a budget.

Use it when

  • Sorted input and a pair or triplet condition
  • Rewriting an array in place
  • Comparing from both ends, as in palindromes

Avoid it when

  • Unsorted input where original indices matter - use a hash map
  • No comparison tells you which pointer to move

Complexity cheatsheet

OperationTimeSpace
Converging pointersO(n)O(1)
Check every pairO(n²)O(1)
Sort first, then pointersO(n log n)O(1) to O(n)

Often confused with: sliding window - also two indices, but it tracks a running total or count for the range between them.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

sortedpairin placepalindromeremove duplicates
  • n ≤ 10⁵ rules out checking every pair
  • O(1) extra space rules out a hash map
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • while left <= right lets an element pair with itself
  • Running it on unsorted input
  • Moving both pointers after a mismatch can skip the only valid pair

Edge cases to test

  • An empty or single-element array
  • No valid pair
  • Duplicates like [2, 2, 3] with target 4 - returns [0, 1]
Interview tip: Explain why each move is safe: “13 plus the smallest value is already too big, so 13 can’t be in any answer.”
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

nums = [1, 4, 6, 8, 11], target = 10. The first check is 1 + 11 = 12. What happens next?

06

Practice

Apply what you just watched on real interview problems.

Generate Two Pointers problem in AI DSA Practice →
  • BeginnerValid PalindromeLeetCode 125 ↗ (opens in a new tab)
  • Intermediate3SumLeetCode 15 ↗ (opens in a new tab)
  • AdvancedTrapping Rain WaterLeetCode 42 ↗ (opens in a new tab)
TriesSliding Window