DAY 7 / 30 · WEEK 1 REVIEW

Week 1 Review

Goal of the day

Concept

Think back to the four analogies from this week: the librarians walking toward each other on a sorted shelf, the picture frame sliding along a strip of numbers, the coat-check ticket that jumps straight to your coat. Different pictures, but the same move every time: a brute-force nested loop (O(n²)) collapsed to one pass (O(n)) by remembering something as you go — a running number, two positions, or a full lookup table. The skill that actually matters in an interview isn't memorizing five templates — it's recognizing which thing you need to remember for the problem in front of you. That recognition is what today trains.

Diagram: which pattern do I reach for?

Max/min/target over a CONTIGUOUS range (subarray/substring)? Sliding Window (Day 5) Compact / dedupe an array, IN PLACE? Two Pointer read/write (Day 4) SORTED array, looking for a pair? Two Pointer converging (Day 4) "Have I seen this?" / count occurrences, ANY order? Hash Map / Set (Day 6) Check these top to bottom — the first "yes" is usually your pattern.

Interactive visualization: combining two patterns

Subarray Sum Equals K blends this week's two biggest ideas: a running sum (like sliding window) AND a hash map of sums seen so far (like Day 6) — this is what "mixed" problems look like in real interviews.

current element match found — counts a subarray not the current element
Press Play or Step to begin.

Week 1 cheat sheet — code templates

// ARRAY BASICS (Day 1): push/pop O(1) end, unshift/shift O(n) front
arr.push(x); arr.pop(); arr.unshift(x); arr.shift();

// STRINGS (Day 3): immutable — always build a new one
const newStr = 'x' + str.slice(1);

// TWO POINTER — converging (Day 4): array must be SORTED
let left = 0, right = arr.length - 1;
while (left < right) {
  const sum = arr[left] + arr[right];
  if (sum === target) { /* found */ break; }
  else if (sum < target) left++;
  else right--;
}

// TWO POINTER — read/write (Day 4): in-place compaction
let write = 0;
for (let read = 1; read < arr.length; read++) {
  if (arr[read] !== arr[write]) { write++; arr[write] = arr[read]; }
}

// SLIDING WINDOW — fixed size (Day 5)
let windowSum = 0;
for (let i = 0; i < k; i++) windowSum += arr[i];
for (let i = k; i < arr.length; i++) windowSum += arr[i] - arr[i - k];

// SLIDING WINDOW — variable size (Day 5)
let left = 0;
for (let right = 0; right < arr.length; right++) {
  // expand: fold arr[right] into your running state
  while (/* window invalid */ false) { left++; /* shrink */ }
}

// HASH MAP — "have I seen this?" (Day 6)
const seen = new Map();
for (let i = 0; i < arr.length; i++) {
  const complement = target - arr[i];
  if (seen.has(complement)) { /* found pair */ }
  seen.set(arr[i], i);
}

Dry run: subarraySum([1, 2, 3], k = 3)

inumsumsum − kIn map?count
0map = {0: 1}0
011−2No0
1230Yes (count 1)1
2363Yes (count 1)2

Two subarrays sum to 3: [1,2] and [3] — found by checking, at each point, whether the running sum minus k has been seen before. The {0: 1} starting entry handles subarrays that start at index 0 exactly.

Complexity — everything from Week 1, at a glance

PatternTimeSpaceReplaces
Array index / push / popO(1)O(1)
Array unshift / shift / splice(middle)O(n)O(1)
Two pointer (either shape)O(n)O(1)O(n²) nested loop
Sliding window, fixed sizeO(n)O(1)O(n·k) resumming
Sliding window, variable sizeO(n)O(1) or O(window)O(n²) or worse substring checks
Hash map / set lookupO(1) averageO(n)O(n) linear scan per lookup

Interview corner

How to talk through it out loud: on a review problem, narrate the elimination, not just the answer — "this isn't sorted, so converging two-pointer is out; it's asking about a contiguous range, so I'll reach for sliding window first." Showing the patterns you ruled out, and why, proves you're recognizing the problem rather than pattern-matching a keyword you memorized.

5 mixed practice problems

1. Move Zeroes (Easy — LeetCode: Move Zeroes)

Move all zeroes in an array to the end, in place, while keeping the relative order of the non-zero elements.

2. Missing Number (Easy — LeetCode: Missing Number)

Given an array containing n distinct numbers from 0 to n, find the one number missing from the range.

3. Intersection of Two Arrays (Easy — LeetCode: Intersection of Two Arrays)

Given two arrays, return their intersection — each element in the result must be unique.

4. Longest Repeating Character Replacement (Medium — LeetCode: Longest Repeating Character Replacement)

Given a string and an integer k, you may replace up to k characters with any other character. Find the length of the longest substring of a single repeated character you can achieve.

5. Subarray Sum Equals K (Medium — LeetCode: Subarray Sum Equals K)

Given an array of integers and an integer k, return the total number of contiguous subarrays whose sum equals k.

Quiz

1. "Find the longest substring with at most 2 distinct characters" is a signal for which pattern?

2. "Does this array contain any duplicate values?" is best solved with:

3. Converging two pointers for a pair-sum problem requires the input to be:

4. Subarray Sum Equals K (today's visualization) combines which two ideas?

5. When you don't instantly recognize a problem's pattern, what's the safest first move?