Introduction
The Stock Span Problem involves finding the span of stock prices for each day.
The span of a stock’s price today is:
Number of consecutive days before todaywhere price was less than or equal to today's priceThe task is to:
- calculate span for every day
This problem is one of the most important applications of:
Monotonic StackThis problem helps in understanding:
- stack optimization
- previous greater element
- span calculation
- efficient traversal
Example
Input:prices =
[100,80,60,70,60,75,85]
Output: [1,1,1,2,1,4,6]
Explanation: 100 → span 1
80 → span 1
70 → spans:
60,70
85 → spans:
80,60,70,60,75,85
Constraints
1 <= prices.length <= 10^51 <= prices[i] <= 10^5Approach 1 : Brute Force Traversal
Explanation
The simplest way is:
- Traverse each day
- Search previous days
- Count consecutive smaller prices
This works but repeated searching becomes inefficient.
Steps
- Traverse prices.
- Move left side.
- Count valid consecutive days.
- Store span.
- Return result array.
Dry Run
Input:[100,80,60,70]
100: Span = 1
80: No previous smaller chain Span = 1
70: 60 <= 70 Span = 2
Result: [1,1,1,2]
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)
Explanation:
Previous days are repeatedly searched.
Space Complexity: O(1) Explanation:
Only output array is used.
Approach 2 : Monotonic Stack
Explanation
The optimized solution uses:
Monotonic Decreasing StackIdea:
- stack stores indices
of greater stock prices - remove smaller prices
while traversing
Process:
- Traverse prices
- Remove smaller/equal prices
- Remaining top:
- previous greater element
- Calculate span
This avoids repeated searching.
Steps
- Create stack.
- Traverse prices.
- Remove smaller prices.
- Calculate span.
- Push current index.
Dry Run
Input:[100,80,60,70]
Stack: []
100: Span = 1 Push index 0
80: Previous greater:
100 Span = 1
70: Remove 60
Previous greater:
80
Span = 2
Result:
[1,1,1,2]
Monotonic Stack Code
Complexity Analysis
Time Complexity: O(n)
Explanation:
Each index is pushed and popped once.
Space Complexity: O(n) Explanation:
Stack stores indices.
Edge Cases
- Strictly increasing prices
- Strictly decreasing prices
- Equal prices
- Single day
- Large input size
Why This Problem is Important
Stock Span Problem helps in understanding:
- Monotonic stacks
- Previous greater element
- Efficient span calculation
- Stack optimization
- Linear traversal
It is one of the most important monotonic stack interview problems.
Real-World Applications
Stock span concepts are used in:
- Financial analysis
- Trading systems
- Market trend analysis
- Performance monitoring
- Historical pattern tracking
Common Mistakes
- Incorrect stack condition
- Forgetting equal values
- Wrong span calculation
- Using values instead of indices
Interview Tips
Interviewers often expect:
- Monotonic stack explanation
- Previous greater reasoning
- Span calculation understanding
Always explain:
- why indices are stored
- how previous greater element helps
- why each index is processed once
Related Questions
- Next Greater Element
- Daily Temperatures
- Largest Rectangle in Histogram
- Trapping Rain Water
- Online Stock Span
Final Takeaway
The Stock Span Problem is a fundamental monotonic stack problem that teaches efficient span calculation and previous-greater optimization techniques. Understanding this problem builds a strong foundation for advanced stack and linear traversal interview problems.