The Stock Span Problem finds, for each day, how many consecutive 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 keep track of 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 higher than the current day's price, helping to 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; otherwise, span = current day index + 1.
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
What does the Stock Span Problem calculate for each day?
ATotal sum of stock prices till current day
BNumber of consecutive days with stock price less than or equal to current day
CMinimum stock price till current day
DMaximum stock price till current day
✗ Incorrect
The problem finds how many consecutive days had prices 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
A stack helps track previous higher prices and calculate spans in O(n) time.
When calculating span, if the stack is empty, what is the span for the current day?
A1
B0
CCurrent day index + 1
DCurrent day index
✗ Incorrect
If no previous higher price exists, span is all days till current day, which is index + 1.
What happens to stack elements when a new day's price is higher than the top of the stack's price?
AIgnore the new price
BPush new price without popping
CClear the stack completely
DPop elements until top has higher price or stack is empty
✗ Incorrect
We pop smaller or equal prices to find the last higher price for span calculation.
What is the overall time complexity of the stack-based Stock Span solution?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Each element is pushed and popped at most once, making the solution linear time.
Explain how a stack helps solve the Stock Span Problem efficiently.
Think about how you find the last day with a higher price before the current day.
You got /4 concepts.
Describe the step-by-step process to calculate the span for each day using a stack.
Imagine checking previous days one by one but using a stack to skip unnecessary checks.
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 minimum stock price in the entire list
D. The difference between the current day's price and the previous day's price
Solution
Step 1: Understand the problem definition
The Stock Span Problem finds how many consecutive days before today had prices less than or equal to today's price.
Step 2: Clarify what is calculated for each day
For each day, it counts days backward until a higher price is found or start is reached.
Final Answer:
The number of consecutive days with price less or equal to current day -> Option A
Quick Check:
Span counts consecutive lower/equal prices before today [OK]
Hint: Span counts days with prices ≤ current day price [OK]
Common Mistakes:
Confusing span with max or min price
Thinking span is price difference
Ignoring consecutive days condition
2. Which of the following JavaScript code snippets correctly initializes an empty stack for solving the Stock Span Problem?
easy
A. const stack = 0;
B. const stack = {};
C. const stack = [];
D. const stack = null;
Solution
Step 1: Identify the correct data structure for stack
In JavaScript, a stack is usually implemented as an array to use push and pop methods.
Step 2: Check initialization syntax
An empty array is initialized with [] which is correct for a stack.
Final Answer:
const stack = []; -> Option C
Quick Check:
Stack uses array [] in JavaScript [OK]
Hint: Use array [] to create stack in JavaScript [OK]
Common Mistakes:
Using object {} instead of array
Assigning null or number instead of array
Forgetting to initialize stack before use
3. What is the output of the stock span array for the prices [100, 80, 60, 70, 60, 75, 85] using the Stock Span Problem algorithm?
medium
A. [7, 6, 5, 4, 3, 2, 1]
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 price
Day 0: 100 -> span=1 (no previous days) 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, 60 -> span=4 (days 4,3,2,1) Day 6: 85 > 75, 70, 60, 80 -> span=6 (days 5,3,2,1,0)
Step 2: Write the 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: Count consecutive previous days with price ≤ current [OK]
Common Mistakes:
Counting all previous days instead of consecutive
Mixing up greater and less conditions
Incorrectly including days with higher prices
4. Identify the error in the following JavaScript code snippet for the Stock Span Problem:
function calculateSpan(prices) {
const stack = [];
const span = [];
for (let i = 0; i < prices.length; i++) {
while (stack.length > 0 && prices[stack[stack.length - 1]] <= prices[i]) {
stack.pop();
}
span[i] = stack.length === 0 ? i + 1 : i - stack[stack.length - 1];
stack.push(i);
}
return span;
}
medium
A. The span calculation formula is incorrect; it should be i + stack[stack.length - 1]
B. There is no error; the code correctly calculates the stock span
C. The stack should store prices, not indices
D. The comparison operator in while loop should be <= instead of <
Solution
Step 1: Analyze the while loop condition
The loop pops indices while the price at top index is less than or equal to current price, which is correct for span calculation.
Step 2: Check span calculation formula
If stack is empty, span is i+1 (all previous days), else difference between current index and top index in stack, which is correct.
Step 3: Verify stack usage
Stack stores indices, which is standard to access prices array correctly.
Final Answer:
There is no error; the code correctly calculates the stock span -> Option B
Quick Check:
Code matches standard stock span algorithm [OK]
Hint: Stack stores indices; span formula uses index difference [OK]
Common Mistakes:
Changing < to <= breaks correct span logic
Storing prices instead of indices causes wrong indexing
Incorrect span formula leads to wrong results
5. Given the stock prices array [10, 4, 5, 90, 120, 80], how would the Stock Span Problem algorithm change if we want to find the span of days with prices strictly less than the current day's price (not less than or equal)? What is the resulting span array?
hard
A. [1, 1, 2, 4, 5, 1]
B. [1, 1, 2, 3, 4, 1]
C. [1, 1, 1, 4, 5, 1]
D. [1, 2, 3, 4, 5, 6]
Solution
Step 1: Understand the strict less than condition
Change the comparison in the algorithm from ≤ to <, so only days with price strictly less count towards span.
Step 2: Calculate spans with strict less than
Day 0: 10 -> span=1 Day 1: 4 < 10 -> span=1 Day 2: 5 > 4 -> span=2 (days 1 and 2) Day 3: 90 > 5,4,10 -> span=4 (days 0,1,2,3) Day 4: 120 > 90 -> span=5 (days 0 to 4) Day 5: 80 < 120 -> span=1
Final Answer:
[1, 1, 2, 4, 5, 1] -> Option A
Quick Check:
Strict less than span matches [1,1,2,4,5,1] [OK]
Hint: Use < instead of ≤ in comparison for strict span [OK]
Common Mistakes:
Not changing comparison operator correctly
Counting equal prices in span incorrectly
Miscounting consecutive days with strict condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.