Online Stock Span - Problem
Design a Real-Time Stock Span Calculator
You're building a financial analytics tool that needs to calculate the stock span for each day's trading data in real-time. The span of a stock's price on any given day is defined as the maximum number of consecutive days (including today and going backward) for which the stock price was less than or equal to today's price.
Problem: Implement a
Example walkthrough:
• Day 1: Price = 100, Span = 1 (only today)
• Day 2: Price = 80, Span = 1 (80 ≤ 80, but 80 > 100 from day 1)
• Day 3: Price = 60, Span = 1 (similar logic)
• Day 4: Price = 70, Span = 2 (70 ≤ 70 for day 4, and 70 > 60 for day 3)
• Day 5: Price = 110, Span = 5 (110 ≥ all previous prices)
Key Challenge: Process prices in a streaming fashion - you can't see future prices, only past ones!
You're building a financial analytics tool that needs to calculate the stock span for each day's trading data in real-time. The span of a stock's price on any given day is defined as the maximum number of consecutive days (including today and going backward) for which the stock price was less than or equal to today's price.
Problem: Implement a
StockSpanner class that processes daily stock prices one by one and returns the span for each day.Example walkthrough:
• Day 1: Price = 100, Span = 1 (only today)
• Day 2: Price = 80, Span = 1 (80 ≤ 80, but 80 > 100 from day 1)
• Day 3: Price = 60, Span = 1 (similar logic)
• Day 4: Price = 70, Span = 2 (70 ≤ 70 for day 4, and 70 > 60 for day 3)
• Day 5: Price = 110, Span = 5 (110 ≥ all previous prices)
Key Challenge: Process prices in a streaming fashion - you can't see future prices, only past ones!
Input & Output
example_1.py — Basic Usage
$
Input:
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
[[], [100], [80], [60], [70], [60], [75], [85]]
›
Output:
[null, 1, 1, 1, 2, 1, 4, 6]
💡 Note:
Day 1: price=100, span=1 (only current day). Day 2: price=80, span=1 (80 < 100). Day 3: price=60, span=1 (60 < 80). Day 4: price=70, span=2 (70≥60 for 1 day + current day). Day 5: price=60, span=1 (60 < 70). Day 6: price=75, span=4 (75≥60,60,70,current = 4 days). Day 7: price=85, span=6 (85≥75,60,70,60,current + 80 day = 6 days).
example_2.py — Increasing Prices
$
Input:
["StockSpanner", "next", "next", "next", "next"]
[[], [31], [41], [48], [59]]
›
Output:
[null, 1, 2, 3, 4]
💡 Note:
With strictly increasing prices, each day's span includes all previous days: 31(span=1), 41≥31(span=2), 48≥41,31(span=3), 59≥48,41,31(span=4).
example_3.py — Decreasing Prices
$
Input:
["StockSpanner", "next", "next", "next", "next"]
[[], [100], [90], [80], [70]]
›
Output:
[null, 1, 1, 1, 1]
💡 Note:
With strictly decreasing prices, each day's span is always 1 because no previous day has a price ≤ current day's price.
Constraints
- 1 ≤ price ≤ 105
- At most 104 calls will be made to next
- All prices are positive integers
- The StockSpanner object will be instantiated and called as such: obj = StockSpanner(), param_1 = obj.next(price)
Visualization
Tap to expand
Understanding the Visualization
1
Maintain Decreasing Stack
Keep stack of (price, span) pairs in decreasing order of prices
2
Process New Price
When new price arrives, pop all stack elements with price ≤ current price
3
Calculate Span
Sum the spans of all popped elements plus 1 for current day
4
Update Stack
Push (current_price, calculated_span) to maintain monotonic property
Key Takeaway
🎯 Key Insight: Just like taller mountain peaks block the view to shorter peaks behind them, higher stock prices make lower previous prices irrelevant for future span calculations. The monotonic stack efficiently maintains only the 'significant' peaks!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code