Challenge - 5 Problems
Stock Span Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Stock Span Calculation
What is the output of the following code that calculates the stock span for given prices?
DSA Python
def calculate_span(prices): stack = [] span = [0] * len(prices) for i, price in enumerate(prices): while stack and prices[stack[-1]] <= price: stack.pop() span[i] = i + 1 if not stack else i - stack[-1] stack.append(i) return span prices = [100, 80, 60, 70, 60, 75, 85] print(calculate_span(prices))
Attempts:
2 left
💡 Hint
Remember the span counts how many consecutive days before the current day had prices less than or equal to today's price.
✗ Incorrect
The code uses a stack to keep track of indices of days with higher prices. For each day, it pops days with prices less or equal to current price, then calculates span as distance from last higher price day or start.
🧠 Conceptual
intermediate1:30remaining
Understanding Stack Usage in Stock Span
Why do we use a stack to solve the Stock Span Problem efficiently?
Attempts:
2 left
💡 Hint
Think about how the stack helps avoid checking all previous days repeatedly.
✗ Incorrect
The stack stores indices of days with prices greater than the current day's price, allowing us to quickly find the nearest higher price day to calculate the span without scanning all previous days.
🔧 Debug
advanced2:00remaining
Identify the Error in Stock Span Code
What error does the following code produce when run?
DSA Python
def calculate_span(prices): stack = [] span = [0] * len(prices) for i, price in enumerate(prices): while stack and prices[stack[-1]] < price: stack.pop() span[i] = i + 1 if not stack else i - stack[-1] stack.append(i) return span prices = [100, 80, 60, 70, 60, 75, 85] print(calculate_span(prices))
Attempts:
2 left
💡 Hint
Check the condition inside the while loop carefully.
✗ Incorrect
The code uses '<' instead of '<=' in the while loop condition, so it does not pop indices with prices equal to current price, causing incorrect span calculation.
❓ Predict Output
advanced1:30remaining
Output of Stock Span with All Increasing Prices
What is the output of the stock span calculation for strictly increasing prices?
DSA Python
def calculate_span(prices): stack = [] span = [0] * len(prices) for i, price in enumerate(prices): while stack and prices[stack[-1]] <= price: stack.pop() span[i] = i + 1 if not stack else i - stack[-1] stack.append(i) return span prices = [10, 20, 30, 40, 50] print(calculate_span(prices))
Attempts:
2 left
💡 Hint
In strictly increasing prices, each day's price is higher than all previous days.
✗ Incorrect
Since each price is higher than all previous prices, the span for each day is the count of all previous days plus the current day, resulting in increasing span values.
🧠 Conceptual
expert1:30remaining
Time Complexity of Stock Span Algorithm
What is the time complexity of the stock span algorithm using a stack for n prices, and why?
Attempts:
2 left
💡 Hint
Consider how many times each price index is pushed and popped from the stack.
✗ Incorrect
Each price index is pushed once and popped at most once, so the total operations are proportional to n, making the algorithm O(n).