Stock Spanner
Design a StockSpanner class that collects daily stock prices and, for each new price, reports the span: the number of consecutive days (ending today) on which the price was less than or equal to today's price. You don't receive all prices at once, they arrive one day at a time via next().
This is a premium problem.
Unlock Stock Spanner with both full courses, DSA and System Design, and every other premium problem.
Problem
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
Input
A series of calls to `next(price)`, where each price is a positive integer. The class is initialized with no arguments.
Output
For each call to `next(price)`, return the span, the count of the most recent consecutive days (including today) where the price was ≤ today's price.
Examples
Input: StockSpanner(); next(100); next(80); next(60); next(70); next(60); next(75); next(85)
Output: [1, 1, 1, 2, 1, 4, 6]
Day 1: price=100, no prior days → span=1. Day 4: price=70 ≥ price on day 3 (60) → extend back. Day 6: price=75 ≥ 60, 70, 60, but 75 < 80, so span=4. Day 7: price=85 ≥ 75, 60, 70, 60, 80... wait. 85 ≥ 80? Yes. So span=6.
The brute-force approach
Store every price. For each new price, scan backward through stored prices until you find one larger than today's price. The span is the distance back to that previous larger price.
prices = []
def next(price):
prices.append(price)
span = 1
i = len(prices) - 2 # start from yesterday
while i >= 0 and prices[i] <= price:
span += 1
i -= 1
return spanIn the worst case (prices always increasing), every new day scans all previous prices. O(n) per query and O(n²) total over n queries. A monotonic stack collapses runs of consecutive non-dominant prices into a single stored entry, so each day is processed in amortized O(1).
Spotting the pattern
This is a Stack problem. The key question to ask yourself:
How do I count consecutive non-dominant days without scanning all the way back every time?
Answering that is where it clicks, and it's exactly what the guided walkthrough below builds with you: the pattern reasoning, a progressive hint ladder that never spoils the answer, a row-by-row dry run, the optimized solution, and an in-browser editor to run your code against real test cases.
Unlock the full guided solution
The Stack walkthrough for Stock Spanner: the progressive hint ladder, a row-by-row dry run, the optimized code, and an in-browser runner, plus both full courses, DSA and System Design, and every other premium problem.
Already finished Big-O and Hash Maps free? This picks up right where the path leads: same format, harder patterns, the ones that show up in every FAANG-style screen.
What you get
- ✓The full DSA course: concept lessons, drills, graded tests, and cold-read capstones for every pattern
- ✓The full System Design course: 20 building-block units for the design round, included at no extra cost
- ✓Stop going blank on algorithm problems. The hint ladder walks you to the answer without spoiling it.
- ✓Actually internalize the trace: dry runs walk state row by row so it clicks before you write a line
- ✓Know when to reach for each pattern. Every problem names the trigger so you spot it next time.
- ✓Write and run JS or Python in the browser with real test cases on every problem
- ✓Access every new problem, unit, and pattern guide as they ship weekly
“This app finally made it click. I tried NeetCode, CTCI, and YouTube for years and still couldn't solve Two Sum. The beginner mental models for the patterns are genuinely so helpful. THANK YOU.”
Full access
$49.99 / year
or $9.99 / month
Now includes a C++ runner alongside JavaScript and Python.
LeetCode Premium is $159/year and doesn't teach you anything.
Less than one Udemy course, with guided problems instead of passive lectures.
One extra month of job searching costs more than a full year here.
- ✓Both courses, every unit: DSA + System Design
- ✓All 150 problems across 33 patterns, plus every new one
- ✓New problems and units weekly
- ✓Cancel anytime
Cancel anytime. Not useful within 7 days? Email for a full refund. Secured by Stripe.