TSkills PlaygroundALGORITHMS / 05
INTERACTIVE LESSON

One scan. Two positions. Clear progress.

Two
Pointers.

Use two indexes to cut away impossible work. The essential question is simple: after comparing the current values, which pointer can safely move?

Two pointers

Timed TypeScript practice

Two pointers

Write your solution, use the timer, then validate TypeScript types and syntax. Your code is never executed.

30:00
Strict TypeScript validation · no execution

Performance, step by step

Two pointers

Palindrome check

O(n) time · O(1) space

The left and right pointers move inward, so each character is inspected at most once.

Remove duplicates in a sorted array

O(n) time · O(1) extra space

A read pointer scans every value once while a write pointer preserves each new value in place.

Intersection of sorted arrays

O(n + m) time

At each comparison, advance the pointer holding the smaller value. Neither pointer ever moves backward.

Loop invariant

A correctness tool

State what is already true before each iteration. Then choose pointer moves that preserve that statement and make progress.

LESSON05Progress by design

Three movement patterns

Choose the choreography.

The data and goal decide where each pointer begins and when it advances.

01 / INWARD

Meet in the middle

Start at opposite ends. Compare, then move both pointers closer together.

Palindromes · pair sums
02 / SLOW + FAST

Read and write

One pointer explores every item; the other marks where the next kept result belongs.

In-place filtering · deduping
03 / PARALLEL

Walk two inputs

Keep one pointer in each sorted sequence. Advance the one that is behind.

Intersections · merging

Pattern 01 · inward pointers

Test a palindrome.

Ignore punctuation and casing, then compare the outside characters. A mismatch settles the answer immediately.

CLEANED INPUTneveroddoreven
0n1e2v3e4r5o6d7d8o9r10e11v12e13n
n = n · both pointers may move inward.

Pattern 02 · slow + fast

Keep unique values.

The read pointer always advances. The write pointer moves only when a new value deserves a place in the compacted prefix.

01112232435464
COMPACTED PREFIX[1]
Skip it · 1 matches the last kept value, so only read advances.

Pattern 03 · parallel pointers

Intersect sorted arrays.

Discard only the smaller current value: it cannot appear later in the other sorted input.

12379
13478

INTERSECTION [1]

Match: save this value and advance both pointers.
MAKE PROGRESS

Every branch must advance at least one pointer. That prevents a stalled loop and bounds the work.

NAME THE INVARIANT

Say what has already been processed or proved. It makes each pointer move easier to justify.

CHECK INPUT PROMISES

Sorted inputs, permitted characters, and in-place constraints change the right implementation.