Excellent — this is the final and most interview-dense day for lists.
If you master DAY 3, list problems stop being “questions” and become patterns you recognize instantly.
🔥 LIST MASTERY — DAY 3
Prefix Sums, Kadane’s Algorithm, Advanced In-Place Tricks & Rapid Interview Drills




🧠 DAY 3 GOAL
By the end of today, you will:
- Know prefix sum thinking (huge interview unlock)
- Solve max/min subarray problems instantly
- Handle range queries efficiently
- Recognize when O(n²) → O(n) is possible
- Pass rapid-fire list interviews confidently
1️⃣ PREFIX SUMS — THE MOST UNDERRATED PATTERN
Used when:
- Range sums
- Subarrays
- Cumulative values
- Optimization from brute force
🔑 Core Idea
Given list:
nums = [2, 4, 1, 3]
Prefix sum:
prefix = [0, 2, 6, 7, 10]
Meaning:
sum(nums[i:j]) = prefix[j] - prefix[i]
🧠 This eliminates nested loops.
🔥 Q1. Range Sum Query
nums = [1,2,3,4]
sum(1..3) → 2+3+4 = 9
Precompute once
prefix = [0]
for x in nums:
prefix.append(prefix[-1] + x)
Query
def range_sum(i, j):
return prefix[j+1] - prefix[i]
✔ O(1) per query
✔ Interviewers love this
2️⃣ SUBARRAY SUM EQUALS K (CLASSIC)
🔥 Q2. Count Subarrays with Sum = K
nums = [1,1,1], k = 2 → 2
❌ Brute Force
- Nested loops
- O(n²)
✅ Optimal (Prefix Sum + HashMap)
from collections import defaultdict
def subarray_sum(nums, k):
count = 0
curr = 0
seen = defaultdict(int)
seen[0] = 1
for x in nums:
curr += x
count += seen[curr - k]
seen[curr] += 1
return count
🧠 Interview explanation:
- Track cumulative sum
- Look back using hash map
- O(n) time
3️⃣ KADANE’S ALGORITHM (ABSOLUTE MUST)
🔥 Q3. Maximum Subarray Sum
[-2,1,-3,4,-1,2,1,-5,4] → 6
(subarray: [4,-1,2,1])
Kadane’s Algorithm
def max_subarray(nums):
max_sum = curr = nums[0]
for x in nums[1:]:
curr = max(x, curr + x)
max_sum = max(max_sum, curr)
return max_sum
✔ O(n)
✔ O(1) space
🧠 Interview Explanation (Say This)
“At each position, I decide whether to extend the previous subarray or start fresh.”
This line alone impresses interviewers.
4️⃣ VARIANTS OF KADANE (INTERVIEW DEPTH)
🔥 Maximum Product Subarray
(Track min & max due to negatives)
🔥 Circular Maximum Subarray
(Max of normal Kadane vs total − min subarray)
Knowing these gives senior-level signal.
5️⃣ PREFIX VS SLIDING WINDOW (IMPORTANT DISTINCTION)
| Problem | Use |
|---|---|
| Fixed size window | Sliding window |
| Variable sum window | Prefix sum |
| Negative numbers | Prefix sum |
| Only positives | Sliding window |
Interviewers expect you to choose correctly.
6️⃣ ADVANCED IN-PLACE TRICKS
🔥 Q4. Rearrange Array Alternating Pos/Neg
[-1, 2, -3, 4] → [2, -1, 4, -3]
Pattern:
- Partition first
- Then interleave
Shows deep list control.
🔥 Q5. Find Missing Number (0..n)
[3,0,1] → 2
Optimal (Math)
def missing(nums):
n = len(nums)
return n*(n+1)//2 - sum(nums)
🔥 Q6. Find Duplicate (Cycle Detection)
Treat list as linked list
(Floyd’s algorithm — advanced interview)
7️⃣ WHEN NOT TO USE LISTS 🚨
Replace list with:
set→ membership heavydeque→ frequent front popsarray/ NumPy → numeric heavy- generator → streaming
Saying this shows engineering maturity.
🔥 25 RAPID-FIRE LIST INTERVIEW QUESTIONS
Answer instantly:
- Why
appendis amortized O(1)? - Why
insert(0,x)is slow? appendvsextend?- Why
list * ndangerous? - Shallow vs deep copy?
- Why slicing costs memory?
- Two pointer use cases?
- Sliding window vs prefix sum?
- Why
remove()in loop bad? - Kadane intuition?
- Stable sort meaning?
- When to use
sorted()? - Why membership is O(n)?
- How to rotate efficiently?
- Remove duplicates in-place?
- Why
pop()fast at end? - When list beats set?
- Range sum optimization?
- Why generators help?
- When extra memory acceptable?
- Prefix sum with negatives?
- Merge sorted lists?
- In-place vs out-of-place?
- Common list interview trap?
- When NOT to optimize?
If you can explain these calmly → you pass list interviews.
🧠 FINAL LIST MENTAL MODEL
Think in this order:
- Is order important?
- Is in-place required?
- Is it subarray or subsequence?
- Are negatives present?
- Can I reduce nested loops?
- What’s the real complexity?
📝 FINAL LIST ASSIGNMENT (MANDATORY)
1️⃣ Predict output (NO RUNNING)
nums = [1, -1, 1]
print(subarray_sum(nums, 1))
2️⃣ Coding (Classic)
def max_product_subarray(nums):
"""
LeetCode hard-favorite
"""
3️⃣ Explain (Interview style)
Why Kadane’s algorithm works in one pass.
🎯 YOU HAVE COMPLETED LIST MASTERY
You now have:
✔ Internal understanding
✔ Method-level clarity
✔ Pattern recognition
✔ Interview-ready confidence
🔜 NEXT MODULE OPTIONS (Choose One)
👉 “START DICTIONARY MASTERY (Module 3)”
(hash tables, collisions, defaultdict, Counter, LRU patterns)
👉 “START SET & TUPLE MASTERY (Module 4)”
(hashability, immutability, math operations)
👉 “START PYTHON CODING INTERVIEW BLITZ”
(mixed problems, timed, FAANG-style)
Say your choice — we continue immediately 🚀