Stock Span Problem
Keep a stack of indices whose prices strictly decrease from bottom to top. For each day, pop every index whose price is ≤ today's. The top that is left is the closest earlier day with a higher price, so span = i − top (or i + 1 if the stack is empty). Then push today.
A day stays on the stack only while no later day has a price ≥ it. Such a later day hides it from everyone after, so it can never stop a span again.
Problem
You are given the price of a stock on n days in a row, in the array arr.
The span of a day is the number of days in a row, ending with that day and going backwards, on which the price was less than or equal to that day's price. The day itself always counts, so every span is at least 1.
Return the span of every day. Write it as vector<int> calculateSpan(vector<int>& arr).
An integer array arr of length n: the price on each day.
An array of n numbers: the span of each day, in order.
On day 5 the price is 75. Going back: 60, 70, 60 are all ≤ 75, then 80 is higher. So the span is 4 (days 2, 3, 4 and 5). On day 6 the price is 85: everything back to 100 is ≤ 85, so the span is 6.
On day 3 the price is 90, and all three earlier prices are lower, so the span is 4. On day 5 the price 80 is lower than the 120 just before it, so the span is 1.
- • 1 ≤ n ≤ 10^5
- • 1 ≤ arr[i] ≤ 10^5
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
When I walk backwards from day i, what exactly makes me stop?
- 2.
If I knew the index of that stopping day, how would I get the span from it?
- 3.
Does a price equal to today's stop the span, or is it counted?
- 4.
Day j is earlier than day i and has a lower (or equal) price. Can day j ever be the stopping day for a day after i?
- 5.
So which earlier days are still worth remembering at any moment?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each day, step backwards while the price is ≤ today's | O(N²) | O(1) |
| Optimal | Keep a stack of days with decreasing prices; pop everything ≤ today | O(N) | O(N) |
Why does this belong to this pattern?
- “days in a row, going backwards”We need the distance to a boundary on the left — the first day that breaks the run.
- “less than or equal to that day's price”The run stops at the first **strictly greater** price → previous greater element, with `≤` being popped.
- “span of every day”We need this boundary for all n days, so it must be cheap each time → monotonic stack.
This is the anchor of the span sub-pattern. It is the same decreasing stack as Next Greater Element, but read the other way: instead of recording an answer for the popped elements, we record the answer for the new element, using whatever is left on top. The span is just i − prevGreater. The next question (Online Stock Span) has no array to look back into, so it keeps the span inside the stack instead of an index.
Don't make this mistake
Equal prices are part of the span. With <, an equal price stays on the stack and stops the span too early: [5, 5, 5] would give [1, 1, 1] instead of [1, 2, 3].
If every earlier price is ≤ today, the stack is empty and the span covers all days from 0 to i, which is i + 1 days. Calling back() on an empty vector is undefined behaviour.
The span is a distance, so you need to know where the higher price was. Store the index and read the price with arr[st.back()].
Pattern confusion with Next Greater Element: there, when arr[i] pops j, you record nge[j] = arr[i]. Here the question is about the previous greater of i, so the popped days get nothing — the answer for i comes from the top after all the pops.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.