The Stock Span Problem finds, for each day, how many consecutive previous days had stock prices less than or equal to the current day's price.
Click to reveal answer
intermediate
Why do we use a stack to solve the Stock Span Problem?
A stack helps track previous days' prices efficiently, allowing us to find the nearest day with a higher price quickly, which helps calculate the span in O(n) time.
Click to reveal answer
beginner
In the Stock Span Problem, what does each element in the stack represent?
Each element in the stack represents a pair: the index of the day and its stock price, helping to compare current price with previous prices.
Click to reveal answer
intermediate
What is the time complexity of the Stock Span Problem solution using a stack?
The time complexity is O(n) because each element is pushed and popped at most once from the stack.
Click to reveal answer
intermediate
How does the algorithm handle days with prices lower than the current day?
It pops all days from the stack with prices less than or equal to the current day's price to find the nearest higher price day.
Click to reveal answer
What does the stock span for a day represent?
ANumber of days after it with higher price
BDifference between highest and lowest price
CTotal number of days in the data
DNumber of consecutive days before it with price less than or equal to current day
ā Incorrect
The span counts how many consecutive previous days had prices less than or equal to the current day's price.
Which data structure is best suited for solving the Stock Span Problem efficiently?
AArray
BQueue
CStack
DLinked List
ā Incorrect
Stack allows quick access to previous days' prices and efficient span calculation.
What happens when the current day's price is higher than the price at the top of the stack?
APop from stack until top price is higher or stack is empty
BPush current day without popping
CIgnore current day
DClear the entire stack
ā Incorrect
We pop all days with prices less or equal to current to find the nearest higher price day.
What is the initial span value for the first day in the stock span problem?
A1
B0
CPrice of the day
DNumber of days in data
ā Incorrect
The first day always has a span of 1 because no previous days exist.
What is the overall time complexity of the stack-based stock span solution?
AO(log n)
BO(n)
CO(n^2)
DO(1)
ā Incorrect
Each element is pushed and popped at most once, making the solution linear time.
Explain how the stack is used to calculate the stock span for each day.
Think about how to find the nearest previous day with a higher price.
You got /4 concepts.
Describe the step-by-step process of solving the Stock Span Problem using a stack.
Focus on how the stack changes with each day.
You got /5 concepts.
Practice
(1/5)
1. What does the stock span problem calculate for each day in a list of stock prices?
easy
A. The number of consecutive days before the current day with a price less than or equal to the current day's price
B. The maximum stock price in the entire list
C. The difference between the highest and lowest stock price
D. The total sum of stock prices up to the current day
Solution
Step 1: Understand the problem definition
The stock span for a day is the count of consecutive previous days with prices less than or equal to that day's price.
Step 2: Clarify what is being calculated
It is not about maximum price or sums, but about consecutive days with lower or equal prices.
Final Answer:
The number of consecutive days before the current day with a price less than or equal to the current day's price -> Option A
Quick Check:
Stock span = consecutive days with price ⤠current price [OK]
Hint: Span counts consecutive days with price ⤠current day price [OK]
Common Mistakes:
Confusing span with maximum price
Thinking span is total sum of prices
Ignoring consecutive days condition
2. Which Go data structure is best suited to efficiently solve the stock span problem?
easy
A. Stack
B. Array
C. Queue
D. Map
Solution
Step 1: Identify the data structure for tracking previous prices
The stack helps keep track of indices of days with prices to compare efficiently.
Step 2: Understand why stack is efficient
Stack allows popping smaller prices and quickly finding the last higher price day, enabling O(n) time complexity.
Final Answer:
Stack -> Option A
Quick Check:
Stack is used for efficient previous price tracking [OK]
Hint: Use stack to track previous days' prices efficiently [OK]
Common Mistakes:
Using array leads to O(n²) time
Queue does not support backward checks
Map is not suitable for order-based checks
3. What is the output span array for the stock prices [100, 80, 60, 70, 60, 75, 85] using the stock span algorithm?
medium
A. [1, 1, 2, 2, 3, 3, 4]
B. [1, 2, 3, 4, 5, 6, 7]
C. [1, 1, 1, 1, 1, 1, 1]
D. [1, 1, 1, 2, 1, 4, 6]
Solution
Step 1: Calculate span for each day
Day 0: 100 -> span=1 (no previous) Day 1: 80 ⤠100 -> span=1 Day 2: 60 ⤠80 -> span=1 Day 3: 70 > 60 -> span=2 (includes day 2) Day 4: 60 ⤠70 -> span=1 Day 5: 75 > 60, 70 -> span=4 (includes days 4,3,2) Day 6: 85 > 75, 70, 60, 80 -> span=6 (includes days 5,4,3,2,1)
Step 2: Confirm final span array
The spans are [1, 1, 1, 2, 1, 4, 6].
Final Answer:
[1, 1, 1, 2, 1, 4, 6] -> Option D
Quick Check:
Span array matches calculated values [OK]
Hint: Pop smaller prices from stack to count span [OK]
Common Mistakes:
Counting all previous days instead of consecutive
Not popping smaller prices from stack
Confusing span with price values
4. Identify the bug in this Go code snippet for stock span calculation:
func calculateSpan(prices []int) []int {
n := len(prices)
span := make([]int, n)
stack := []int{}
for i := 0; i < n; i++ {
for len(stack) > 0 && prices[stack[len(stack)-1]] <= prices[i] {
stack = stack[:len(stack)-1]
}
if len(stack) == 0 {
span[i] = i + 1
} else {
span[i] = i - stack[len(stack)-1]
}
stack = append(stack, i)
}
return span
}
medium
A. The loop should start from i = 1 instead of 0
B. The comparison operator should be <= instead of < in the inner loop
C. Span array is not initialized with correct size
D. Stack should store prices, not indices
Solution
Step 1: Analyze the inner loop condition
The inner loop pops indices while the price at stack top is less than current price. It should pop while less than or equal to handle equal prices correctly.
Step 2: Understand impact of using < instead of <=
Using < misses popping equal prices, causing incorrect span counts for equal prices.
Final Answer:
The comparison operator should be <= instead of < in the inner loop -> Option B
Quick Check:
Use <= to correctly handle equal prices [OK]
Hint: Use <= in comparison to handle equal prices correctly [OK]
Common Mistakes:
Stack storing prices instead of indices
Incorrect span array size
Starting loop from wrong index
5. Given stock prices [10, 4, 5, 90, 120, 80], how would you modify the stock span algorithm to also return the day index of the last higher price before the current day? What is the output for the last higher price indices array?
hard
A. [ -1, 0, 1, -1, -1, 4 ]
B. [ -1, 0, 0, 2, 3, 3 ]
C. [-1, 0, 0, -1, -1, 4]
D. [ 0, -1, 1, 2, 3, 4 ]
Solution
Step 1: Modify algorithm to track last higher price index
Use stack to store indices; for each day, pop while current price ā„ price at stack top. If stack empty, last higher index is -1; else top of stack is last higher index.
Step 2: Calculate last higher price indices for given prices
Prices: [10,4,5,90,120,80] Day 0: empty -> -1 Day 1: 10 > 4, no pop -> 0 Day 2: 4 <= 5 pop 1, 10 > 5 -> 0 Day 3: 5 <= 90 pop 2, 10 <= 90 pop 0, empty -> -1 Day 4: 90 <= 120 pop 3, empty -> -1 Day 5: 120 > 80, no pop -> 4
Final Answer:
[-1, 0, 0, -1, -1, 4] -> Option C
Quick Check:
Last higher indices track previous greater price day [OK]
Hint: Track last higher price index using stack top or -1 if empty [OK]
Common Mistakes:
Returning span instead of last higher index
Not handling empty stack case (-1)
Confusing last higher index with span count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.