Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Treasury is investigating a suspicious net movement of a fixed amount and wants to know how many stretches of consecutive days in the ledger could explain it. Each day has one net movement: sales push it up, refunds push it down, and a quiet day is zero.
Write count_runs_with_total(movements, target). It takes movements, a list of integer daily net movements in date order (negatives and zeros allowed), and target, an integer, and returns how many contiguous runs of one or more days sum to exactly target.
Function to write
count_runs_with_total(movements: list[int], target: int) -> intThe number of contiguous runs of one or more days whose movements sum to exactly target.
How to approach it
The sum of days i to j is the running total at j minus the running total before i. Count how many times each running total has been reached, and look up today's total minus the target.
Sample cases
+ 3 held back until you submit
refunds make a run net out
[3, 4, -2], [5] and [5, -3, 2, 1] all net to 5; a shrinking window loses the first one at the 7.
Input
Argument 1
[
3,
4,
-2,
5,
-3,
2,
1
]Argument 2
5Returns
3overlapping runs all count
Days 1-2, 2-3 and 3-4 each sum to 2; overlapping runs are separate runs and all three count.
Input
Argument 1
[
1,
1,
1,
1
]Argument 2
2Returns
3target zero counts zero-sum runs
Each 0 day alone, both 0 days, 5 then -5, and the longer runs around them — six runs net to 0.
Input
Argument 1
[
0,
0,
5,
-5
]Argument 2
0Returns
6an empty ledger
No days means no runs: return 0, and never count the empty run as one.
Input
Argument 1
[] (empty list)
Argument 2
10Returns
0Constraints
movements. The empty run never counts, even when target is 0.[1, 1, 1, 1] with target 2 there are 3 runs.target can be negative or zero.movements list returns 0.Worked example
Take [3, 4, -2, 5, -3, 2, 1] with target 5. Keep a running total and a count of every total reached so far, starting with {0: 1} for 'before day one'. The running totals are 3, 7, 5, 10, 7, 9, 10. A run ending today sums to 5 when some earlier total equals today's total minus 5. At the third day the total is 5, and 0 was reached once — that is the run [3, 4, -2]. At the fourth day the total is 10, and 5 was reached once — the run [5]. At the last day the total is 10 again, 5 is still there once — the run [5, -3, 2, 1]. Answer: 3.
The trap: a sliding window that shrinks whenever the sum passes the target only works when every number is positive. Here it reaches 7 after 3 and 4, drops the 3 to get back under 5, and never sees [3, 4, -2] — the -2 that would have fixed it had not arrived yet.
What this tests
Prefix sums with a hash map of counts: LeetCode 'Subarray Sum Equals K' in data clothing. The target is O(n) time and O(n) space — checking every start and end is O(n²) and passes the samples. A data engineer meets it for real in running balances, cumulative metrics and 'when did this net to X' investigations. It is reported in data engineering loops: Paytm data engineering candidates report 'Subarray sum equal to K', and a Goldman Sachs data engineering candidate reports 'find subarray with given sum'.
count_runs_with_total(movements: list[int], target: int) -> intSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.