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
A capacity report shows, for every day of an event stream's history, how long the platform had to wait before traffic climbed past that day's level. A long wait after a peak means the peak was an outlier; a wait of 1 means volume was still rising. The input is one total per day, oldest first.
Write days_until_higher(volumes). It takes a list of daily event volumes as non-negative integers in date order and returns a list of integers of the same length, where position i holds how many days until a strictly higher volume than day i, or 0 if no later day is higher.
Function to write
days_until_higher(volumes: list[int]) -> list[int]A list the same length as the input: days until a strictly higher volume, or 0 when none comes.
How to approach it
Keep a stack of the days still waiting for a higher volume. Each new day answers every waiting day it strictly beats, starting from the top of the stack.
Sample cases
+ 3 held back until you submit
a week of rising and falling volume
One 150 day answers three waiting days at once, including two tied 80 days.
Input
Argument 1
[
120,
90,
95,
130,
80,
80,
150
]Returns
[
3,
1,
1,
3,
2,
1,
0
]a plateau is not a rise
Equal volumes do not count as higher, so each 100 day waits for the 110 day.
Input
Argument 1
[
100,
100,
100,
110
]Returns
[
3,
2,
1,
0
]a peak nobody beats
The 300, 200 and 150 days are never beaten by a later day, so those three stay 0.
Input
Argument 1
[
300,
120,
200,
150
]Returns
[
0,
1,
0,
0
]no days
An empty series returns an empty list, the same length as the input.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
Constraints
[]. Zero is a real volume and follows the same rules.Worked example
Take "a week of rising and falling volume": [120, 90, 95, 130, 80, 80, 150]. Keep a stack of the days still waiting. Day 0 (120) waits: stack [0]. Day 1 (90) is not higher than 120, so it waits too: [0, 1]. Day 2 (95) beats day 1, so day 1 gets 2 - 1 = 1; it does not beat 120, so it waits: [0, 2]. Day 3 (130) beats day 2 (wait 1) and day 0 (wait 3): [3]. Day 4 (80) waits, and day 5 (80) is equal to day 4, not higher, so it waits as well: [3, 4, 5]. Day 6 (150) beats day 5 (wait 1), day 4 (wait 2) and day 3 (wait 3). Day 6 is never beaten and keeps 0. The answer is [3, 1, 1, 3, 2, 1, 0].
The trap is the equal volume. On "a plateau is not a rise", [100, 100, 100, 110], a comparison with >= answers 1 for each of the first three days; the correct answer is [3, 2, 1, 0].
What this tests
The monotonic stack: LeetCode 'Daily Temperatures' (problem 739), the list form of 'Next Greater Element', in data clothing. O(n) time and O(n) space, because every index is pushed once and popped at most once. The forward scan from each day is O(n²): it passes these samples and times out on a long history, worst of all on a steady decline where every scan runs to the end. Candidates report it in a Walmart Global Tech data engineer DSA round and a Flipkart data engineering round. A data engineer meets it when measuring time to recovery in a metric series: how long until throughput, a balance or a backlog passes today's level.
days_until_higher(volumes: list[int]) -> list[int]Submit 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.