Command Palette

Search for a command to run...

Module 2

Arrays

The most common input in coding problems: walk it once, carry a little state, work in place, and recognise Kadane's algorithm.

Beginner 4 lessons 7 problems ~50 min of lessons

Arrays look simple, but most of the patterns in this course start here: one pass with a running state, prefix and suffix thinking, in-place updates, and Kadane's algorithm for the best subarray.

Each problem below starts with the obvious slow solution and asks what repeated work makes it slow. That question, more than any formula, is what turns O(n²) into O(n).

Best after: Big-O and Complexity

Part 1

Learn the ideas

Part 2

Solve the problems

In order of difficulty. Each one shows the pattern it teaches.

  1. Replace an O(n²) look-back with one variable (the cheapest price so far): the purest example of running state.

  2. Three answers to one question: counting with a map, sorting, and the Boyer-Moore vote that needs only two variables.

  3. When values are 0..n, the index itself can act as the hash. Also the arithmetic shortcut, and how to avoid its overflow.

  4. Kadane's algorithm: the best subarray ending here is either this element alone or it plus the best ending just before.

  5. The reversal trick: three in-place reverses rotate an array with O(1) extra space.

  6. The four-boundary technique for walking a matrix layer by layer without revisiting cells.

  7. Using the array itself as a hash table: place each value 1..n at index value − 1, then scan for the first gap.