Skip to the workspace
Pebellslearn by building
Foundations: Complexity & Arrays  ›  Lesson

Two Pointers & Sliding Window

Two Pointer Technique 22 minTwo moving indexes instead of a loop inside a loop

dsa / Two Pointers & Sliding Window

Explore each scene at your own pace.

01 · Fill a 9-minute gap with two tracks

Fill a 9-minute gap with two tracks

Two Pointers & Sliding Window

Definition
The two-pointer technique scans an array with two indexes that only move forward (or towards each other), so each step discards work for good and a pass that looked quadratic finishes in linear time.
In plain words
Instead of comparing every pair, put one finger at each end of a sorted row and move them inwards. Each comparison tells you which finger can never be useful again, so you rule out whole groups of pairs at once.

In short: some problems scan an array with one loop nested inside another — O(n²) — but really only need two indexes moving through it together, each one only ever going forward. That's the two-pointer technique: one pointer at each end closing inward, or two pointers (read and write, or window start and end) moving the same direction. Every step discards work for good, so the whole pass is O(n) time and usually O(1) extra space.

Two hands closing in on a shelf of track lengths from opposite ends, each comparison throwing one track away for good, meeting in the middle

Fill a 9-minute gap with two tracks

The obvious way tries every pair — O(n²):

python

The inner loop learns nothing from the sort. Once 2 + 8 is too big, every pair with the 8 is too big too — 2 is the shortest partner it will ever get. Put lo on the shortest track and hi on the longest instead: too small → move lo right; too big → move hi left; exact → done. Each comparison throws one track away for good, so at most n steps — O(n) time, O(1) space:

your code, drawn · array

Each comparison throws away one track for good, which is why the pair search takes at most n steps.

The mistake: trying this on unsorted data. Pointers from both ends only work because the sort guarantees "too big" means everything from here to hi is too big. Sort first (O(n log n) — still beats O(n²)), or keep the original positions by sorting indexes, not values.

What does this print?
mins = [1, 3, 4, 5, 9]
lo, hi = 0, len(mins) - 1
steps = 0
while lo < hi:
    steps += 1
    total = mins[lo] + mins[hi]
    if total == 8:
        break
    if total < 8:
        lo += 1
    else:
        hi -= 1
print(mins[lo], mins[hi], steps)

The same "walk inward" shape reverses a list and checks palindromes — but both ends must start on real items:

broken — fix itIndexError

"level" reads the same both ways, so this should print True. It crashes on the very first comparison instead.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — the library
This piece — session_within(): Finds a run of tracks that fits a time budget.
Scenario A listener taps "30 minutes of music". The session finder slides a window along their queue to find the longest back-to-back run that fits — one O(n) pass instead of checking every start and end.
session_within.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
session_within(durations, limit) → list[start, end) of the longest run of tracks that fits the time limit.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.