Skip to main content
impol.dev

Arrays and Hashing

Array traversal and state

Choose the smallest useful state while scanning an array from left to right.

Lesson 3/19

Sign in to watch

Sign in

Practice

Reference problems

0/2 solved

Notes

Lesson text

Array problems are often state-tracking problems. At each position, decide what the scan has already learned and what the next position will need.

Use a set for seen values, a frequency map for counts, or a map for last positions.

Maintain running values such as a prefix sum, minimum, maximum, count, or best answer.

Preserve order for subarrays and prefix-based logic; a subarray must remain contiguous.

State checklist

Before writing the loop, ask what has been seen, what the next step needs, and whether the answer depends on a prior value or index. Store only that information.

When order matters

Sorting can simplify a minimum or maximum problem when original order is irrelevant. For subsequences, subarrays, and prefix sums, keep the order or store enough information to reconstruct it.