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
Finance wants the best run of the quarter: the stretch of consecutive days over which a merchant account took in the most money, net of refunds and payouts. Individual days can be negative, so the best stretch may cross a bad day that sits between two good ones.
Write best_stretch(daily). It takes a list of [date, net_amount] pairs, one per day in date order, where date is a YYYY-MM-DD string and net_amount is an integer that may be negative or zero. It returns {"start": date, "end": date, "total": int} for the contiguous stretch with the largest total, or None for an empty list.
Function to write
best_stretch(daily: list[list]) -> dict | NoneA dict with the start date, end date and total of the best consecutive stretch, or None.
How to approach it
The best stretch ending today is either today alone or today added to the best stretch ending yesterday. Drop yesterday's stretch only when it is below zero.
Sample cases
+ 3 held back until you submit
a dip inside a winning stretch
The best stretch crosses a -1 day: 4, -1, 5 totals 8, more than either good day alone.
Input
Argument 1
[
[
'2026-03-01',
-3
],
[
'2026-03-02',
4
],
[
'2026-03-03',
-1
],
[
'2026-03-04',
5
],
[
'2026-03-05',
-6
],
[
'2026-03-06',
2
]
]Returns
{
'start': '2026-03-02',
'end': '2026-03-04',
'total': 8
}a stretch that breaks even
The whole first three days and the third day alone both total 3; the earlier start wins.
Input
Argument 1
[
[
'2026-03-01',
2
],
[
'2026-03-02',
-2
],
[
'2026-03-03',
3
],
[
'2026-03-04',
-4
]
]Returns
{
'start': '2026-03-01',
'end': '2026-03-03',
'total': 3
}every day loses money
All negative: the answer is the least-negative day, and -2 twice resolves to the earlier date.
Input
Argument 1
[
[
'2026-03-01',
-5
],
[
'2026-03-02',
-2
],
[
'2026-03-03',
-7
],
[
'2026-03-04',
-2
]
]Returns
{
'start': '2026-03-02',
'end': '2026-03-02',
'total': -2
}no days at all
An empty ledger has no stretch to report, so the function returns None.
Input
Argument 1
[] (empty list)
Returns
None
Constraints
start and end are the dates of its first and last day, equal for a one-day stretch.2, -2, 3 answer all three days (total 3 from day one), not the last day alone, and 4, 0 answers day one alone.None. Dates are returned exactly as given; the list is already in date order with one entry per date.start, end and total. Do not modify the list you were given.Worked example
Take "a dip inside a winning stretch": amounts -3, 4, -1, 5, -6, 2 on 2026-03-01 to 2026-03-06. Keep a running total and restart it only when the stretch so far is below zero. On 2026-03-01 the run is -3, the best so far. On 2026-03-02 the run is negative, so restart: the run is 4 from 2026-03-02, a new best. On 2026-03-03 the run is 4 - 1 = 3, not negative, so keep going. On 2026-03-04 it is 3 + 5 = 8, a new best. Then 8 - 6 = 2 and 2 + 2 = 4, neither better. The answer is start 2026-03-02, end 2026-03-04, total 8: the -1 day was worth crossing.
The trap is the reset. On "a stretch that breaks even" the run is exactly 0 after 2026-03-02; restarting at zero reports 2026-03-03 alone, but the earlier start wins, so the answer is 2026-03-01 to 2026-03-03. And a best total that starts at 0 returns nothing on "every day loses money", where the answer is -2 on 2026-03-02.
What this tests
Kadane's algorithm: LeetCode 'Maximum Subarray' (problem 53) in data clothing, with the index bookkeeping and tie-breaks the textbook version skips. O(n) time and O(1) extra space; trying every start and end is O(n²), fine for a month and hopeless for years of per-minute metrics. 'Maximum Subarray' is tagged for data engineers at Amazon, Google, LinkedIn and Oracle on Exponent. A data engineer meets it when finding the best or worst window in a metric series: peak net inflow, the deepest drawdown, the worst stretch of error-budget burn.
best_stretch(daily: list[list]) -> dict | NoneSubmit 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.