Subarray Sum Equals K
Prefix Map Step by Step
Every subarray ending at j sums to k when an earlier prefix sum equals prefix[j] − k, so a map counts the prefixes.
Step back and forward through the map, then share a link to the step where a lookup failed.
What You Watch While the Map Fills
The array and the map change together on every step.
Every lookup answers one question, which is whether an earlier prefix left exactly k.
CodeStory's visualiser shows the array and the map filling up beside it, key by key. Highlighted code lines follow along, and you can step back, step forward or play.
Open Negatives first, where a sliding window fails but the map still works.
How the Prefix Map Counts Subarrays
Steps below follow the map on your screen.
- 1Start the map with a prefix sum of 0 seen once.
- 2Add the next number to the prefix sum so far.
- 3Look up the prefix sum minus k in the map and add its count to the answer.
- 4Store the new prefix sum in the map and add 1 to its count.
- 5Return the answer after the last number.
Where Subarray Sum Goes Wrong
Both switches live in the visualiser. Flip one on and watch the count break.
- Brute force: every subarrayAdds up every subarray with a live total, so you get about n²/2 additions instead of n lookups.
- Bug: map starts emptyForgets seen = {0: 1}, so every subarray that starts at index 0 goes missing. Load Only the whole array to see it.
Subarray Sum Inputs Worth Trying
Three presets cover negatives, zeros and the empty map bug.
- NegativesA sliding window gets this wrong, because with negatives a bigger window can shrink the sum.
- All zeros, k = 0Every subarray counts, so watch the counts in the map grow.
- Only the whole arraySwitch on the empty map bug here, because the only answer starts at index 0.
Subarray Sum Cost in Time and Space
Interviewers ask why one pass beats checking every subarray, and the visualiser counts real operations.
- Time
- O(n)counted from the lookups and inserts you step through
- Space
- O(n)extra memory for the prefix map
- Pattern
- Arrays & hashingMemory buys back time.
Practise Hashing Beyond Subarray Sum
Watching shows the map and solving trains you to reach for it.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
Sign up for 100 AI credits now and get 100 more every month. Start free in the app
Subarray Sum Visualiser FAQs
Open the visualiser and follow the prefix map key by key.
Open the visualiserWhat does the subarray sum equals k visualiser show?
CodeStory's visualiser shows the array and the prefix map filling up beside it, key by key, with the highlighted line of code. Pause, rewind or play whenever you like.
What is the time complexity of subarray sum?
O(n) time and O(n) space. Operation counts update live as you step, so you see the lookups add up instead of trusting a formula.
Why does a sliding window fail with negative numbers?
Negative numbers break it, because adding a number can shrink the sum. Load Negatives to see the window fail while the prefix map still counts correctly.
Why does my subarray sum code miss subarrays that start at index 0?
Your map needs a prefix sum of 0 seen once before the loop starts. Switch on the empty map bug in CodeStory's visualiser and load Only the whole array to watch the answer vanish.
Can I use my own input?
Yes. Enter your own array and a value for k, and the visualiser fills the prefix map for them.
Is the visualiser free?
Yes. New accounts get 100 AI credits and 100 more every month, and watching an algorithm spends none.
Stuck on Hashing? Get a Free Callback
Tell CodeStory which map step loses you, and a mentor plans what comes next.
- 1CodeStory checks your level, from array basics to interview ready.
- 2CodeStory explains online and campus batches, with timings that suit your mornings or evenings.
- 3You decide. No pressure, no payment on the call.