DSA Factory
All free lessons
Level 1 · Foundations

Free Arrays lessons

35 lessons, about 295 minutes in all. Each teaches one idea with a visual, then gives you one problem built for it. Read the lessons here, then create a free account to solve the problems and keep your progress.

Stage 0What an array is

  1. Positions start at 0Read the first and last values.4 min
  2. The length tells you where things areFind the middle from the length.5 min
  3. Changing a value in placeSwap the first and last values.6 min

Stage 1Traversal

  1. A loop visits every boxAdd up every value with one loop.6 min
  2. Count what matchesCount the even numbers.6 min
  3. Stop when you find itReturn the first position of a value, or −1.7 min
  4. Walk backwardsBuild a reversed copy of a list.7 min

Stage 2Traversal with state

  1. Remember the best so farFind the largest value in one pass.7 min
  2. Two things at onceTrack the smallest and the largest in one pass.6 min
  3. Carry a total forwardBuild the list of running totals.7 min
  4. Compare with the previous boxFind the longest run of equal values.9 min

Stage 5Two pointers

  1. Pair sum in a sorted arrayWalk inward from both ends.8 min
  2. Count pairs below a targetOne check can count a whole group of pairs.10 min
  3. Squares in sorted orderThe biggest is always at one of the ends.10 min
  4. Merge two sorted listsOne pointer in each list; take the smaller.10 min

Stage 6Arrays and hashing

  1. Remember where you first saw itKeep the first position of every value in a map.8 min
  2. Remember the latest positionKeep the last position of every value to spot close repeats.8 min
  3. Only start at the startFind the longest run of consecutive values with a set.10 min
  4. Ask the set in orderFind the smallest positive number that isn't in the list.7 min

Stage 7Prefix sums

  1. Pay once, answer fastBuild running totals once, then answer any range sum with one subtraction.9 min
  2. Left side, right sideUse the total and a running sum to compare both sides of every position.8 min
  3. Same total twiceSpot a stretch that sums to zero by remembering every running total.9 min
  4. Count stretches that hit kCount every run of numbers that adds up to k, with a map of running totals.10 min

Stage 8Sliding window

  1. Slide, don't re-addMove a fixed-size window one step at a time, adding one number and dropping one.8 min
  2. Judge every windowSlide a fixed window and count the ones that pass a test.8 min
  3. Grow, then shrinkA window that stretches on the right and tightens on the left.10 min
  4. Keep the window validFind the longest window that breaks a rule at most k times.10 min

Stage 9Kadane’s algorithm

  1. Extend or start freshFind the largest sum of any run, deciding at each number whether to keep the run going.10 min
  2. Flip the questionThe same walk finds the smallest sum, with max turned into min.6 min
  3. Around the cornerThe best run in a circle is either an ordinary run, or everything except the worst run.10 min
  4. Cheapest so farFind the best buy-then-sell profit by remembering the lowest price seen.8 min

Stage 10Mastery

  1. Balanced bitsThe longest stretch with as many 0s as 1s.12 min
  2. Best window, no repeatsThe largest sum of k in a row, all different.12 min
  3. Widest containerPick two walls that hold the most water.12 min
  4. Best productThe largest product of a run of numbers, negatives included.12 min

There’s more after this

These are the opening stages of Arrays. The full path climbs on to harder problems and a mastery test. Create a free account and we’ll keep your place.

Start the full path free