The Stock Span Problem finds, for each day, the maximum number of consecutive days (including the current day) for which the stock prices were 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 with higher stock prices efficiently, allowing us to find spans in O(n) time by popping smaller or equal prices until a higher price is found.
Click to reveal answer
intermediate
What does each element in the stack represent in the Stock Span Problem?
Each element in the stack stores the index of a day with a stock price greater than the current day's price, helping calculate the span.
Click to reveal answer
intermediate
How is the span for a day calculated using the stack?
Span = current day index - index of last higher price day (top of stack) if stack not empty; else span = current day index + 1.
Click to reveal answer
beginner
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.
Click to reveal answer
What does the Stock Span Problem calculate for each day?
AMaximum stock price till current day
BNumber of consecutive days (including the current day) with stock prices less than or equal to current day
CMinimum stock price till current day
DTotal sum of stock prices till current day
✗ Incorrect
The problem finds the maximum number of consecutive days (including the current day) for which the prices were less than or equal to the current day's price.
What data structure is best suited to solve the Stock Span Problem efficiently?
AStack
BQueue
CArray
DLinked List
✗ Incorrect
Stack helps track previous higher prices and calculate spans in O(n) time.
In the stack used for the Stock Span Problem, what do the stored values represent?
ANumber of days
BStock prices themselves
CSum of stock prices
DIndices of days with higher stock prices
✗ Incorrect
Stack stores indices of days with higher prices to calculate spans.
If the stack is empty when calculating span for a day, what is the span value?
ACurrent day index + 1
B0
C1
DCurrent day index
✗ Incorrect
If no previous higher price exists, span is all days till current day, i.e., index + 1.
What is the time complexity of the stack-based Stock Span solution?
AO(log n)
BO(n^2)
CO(n)
DO(1)
✗ Incorrect
Each element is pushed and popped once, making the solution linear time.
Explain how a stack helps solve the Stock Span Problem efficiently.
Think about how the stack keeps track of previous higher prices.
You got /4 concepts.
Describe the step-by-step process to calculate the span for each day using a stack.
Imagine checking previous days and removing those with lower prices.
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 and including 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 Problem finds, for each day, how many consecutive days before and including the current day had stock prices less than or equal to the current day's price.
Step 2: Clarify what is being counted
It counts days backward until a higher price is found or the start is reached.
Final Answer:
The number of consecutive days before and including the current day with a price less than or equal to the current day's price -> Option A
Quick Check:
Span counts consecutive lower/equal prices [OK]
Hint: Span counts consecutive days with prices ≤ current day price including current day [OK]
Common Mistakes:
Confusing span with maximum price
Counting all previous days instead of consecutive ones
Calculating price differences instead of counts
2. Which Java data structure is most suitable to efficiently solve the Stock Span Problem?
easy
A. Queue
B. ArrayList
C. HashMap
D. Stack
Solution
Step 1: Identify the need for tracking previous prices
The problem requires checking previous days' prices to find the span efficiently.
Step 2: Choose a data structure that supports last-in-first-out access
A stack allows pushing and popping prices or indices to quickly find the last higher price.
Final Answer:
Stack -> Option D
Quick Check:
Stack supports efficient previous day price checks [OK]
Hint: Use stack to track previous prices efficiently [OK]
Common Mistakes:
Using ArrayList which is inefficient for this problem
Choosing HashMap which doesn't maintain order
Using Queue which is FIFO, not suitable here
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, 1, 2, 1, 4, 6]
B. [1, 2, 3, 4, 5, 6, 7]
C. [1, 1, 2, 2, 3, 3, 4]
D. [1, 1, 1, 1, 1, 1, 1]
Solution
Step 1: Calculate span for each day
Day 0: 100, no previous higher price, span=1 Day 1: 80 ≤ 100? Yes, but previous higher price is 100, so span=1 Day 2: 60 ≤ 80? Yes, but previous higher price is 80, so span=1 Day 3: 70 > 60, span=2 (days 2 and 3) Day 4: 60 ≤ 70? Yes, but previous higher price is 70, so span=1 Day 5: 75 > 60 and 70, span=4 (days 2,3,4,5) Day 6: 85 > 75, 70, 60, span=6 (days 1 to 6)
Step 2: Confirm final span array
The span array is [1, 1, 1, 2, 1, 4, 6].
Final Answer:
[1, 1, 1, 2, 1, 4, 6] -> Option A
Quick Check:
Span matches consecutive lower/equal days [OK]
Hint: Count consecutive days with price ≤ current day [OK]
Common Mistakes:
Counting all previous days instead of consecutive
Misapplying stack logic causing wrong spans
Confusing span with price values
4. Identify the error in this Java snippet for Stock Span calculation:
for (int i = 0; i < prices.length; i++) {
while (!stack.isEmpty() && prices[stack.peek()] < prices[i]) {
stack.pop();
}
span[i] = stack.isEmpty() ? i + 1 : i - stack.peek();
stack.push(i);
}
medium
A. The stack should store prices, not indices
B. The span calculation should use i + stack.peek() instead of i - stack.peek()
C. The comparison operator should be <= instead of <
D. The loop should start from i = 1 instead of 0
Solution
Step 1: Analyze the while condition for popping stack
The condition uses < to pop indices with prices less than current price, but it should pop prices less than or equal to current price to correctly count span.
Step 2: Confirm correct comparison operator
Using <= ensures all previous days with price ≤ current are popped, giving correct span.
Final Answer:
The comparison operator should be <= instead of < -> Option C
Quick Check:
Use <= to pop all lower or equal prices [OK]
Hint: Use <= in while to pop all lower or equal prices [OK]
Common Mistakes:
Using < instead of <= causing wrong spans
Storing prices instead of indices in stack
Starting loop at wrong index
5. Given stock prices [10, 4, 5, 90, 120, 80], what is the span array if the stack is implemented to store prices instead of indices?
hard
A. [1, 1, 2, 4, 5, 1]
B. [1, 1, 1, 1, 1, 1]
C. [1, 2, 3, 4, 5, 6]
D. [1, 1, 2, 1, 2, 1]
Solution
Step 1: Understand stack storing prices instead of indices
Without indices, we cannot calculate span as difference of indices; span calculation fails.
Step 2: Resulting span values
Each day is treated as isolated, so span defaults to 1 for all days.
Final Answer:
[1, 1, 1, 1, 1, 1] -> Option B
Quick Check:
Stack must store indices for correct span [OK]
Hint: Stack must store indices, not prices, for correct span [OK]
Common Mistakes:
Storing prices causes incorrect span calculation
Assuming span grows without index info
Ignoring index difference in span formula
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.