Contiguous subarrays with sum k
A water tank logs one signed flow value per minute: positive means water in, negative means water out. An auditor wants to know how many contiguous stretches of minutes had a net flow of exactly k liters. Count every such stretch (non-empty, any length, any starting minute). Flows and k may be zero or negative, so stretches can overlap and shorter stretches inside longer ones count separately. Example: flows = [2, -2, 2, -2], k = 0 gives 4.
Implement
count_net_stretches(flows: list[int], k: int) → intExamples
in
[[2,-2,2,-2],0]out4What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 16 min
solution.py
InputExpectedGot
[[2,-2,2,-2],0]4not run yetsample