← All questions
MediumCoding

Subarray Sum Equals K

Asked atAmazon
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Maintain cumulative prefix sum while scanning.
  2. 02Seed frequency of prefix zero as one.
  3. 03For current sum S, add the previous frequency of S minus k.
  4. 04Increment the frequency of S after counting matches.

Reference answer

Then expect these follow-ups

  • Why does freq zero start at one?

    Tests: prefix sums

  • How would you return one matching range?

    Tests: state storage

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question