0
0
DSA Pythonprogramming~20 mins

Stock Span Problem Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stock Span Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A[1, 1, 1, 2, 1, 4, 6]
B[1, 2, 3, 1, 2, 1, 1]
C[1, 1, 2, 2, 3, 4, 5]
D[1, 1, 1, 1, 1, 1, 1]
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.
🧠 Conceptual
intermediate
1:30remaining
Understanding Stack Usage in Stock Span
Why do we use a stack to solve the Stock Span Problem efficiently?
ATo store all prices in sorted order for binary search.
BTo reverse the prices array for backward traversal.
CTo keep track of indices of days with prices greater than the current day's price for quick span calculation.
DTo hold the span values temporarily before final calculation.
Attempts:
2 left
💡 Hint
Think about how the stack helps avoid checking all previous days repeatedly.
🔧 Debug
advanced
2: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))
AThe code produces incorrect span values due to missing equality in comparison.
BThe output is [1, 1, 1, 3, 1, 4, 6]
CThe output is [1, 1, 1, 1, 1, 1, 1]
DThe output is [1, 1, 1, 2, 1, 4, 6]
Attempts:
2 left
💡 Hint
Check the condition inside the while loop carefully.
Predict Output
advanced
1: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))
A[1, 1, 1, 1, 1]
B[1, 2, 3, 4, 5]
C[5, 4, 3, 2, 1]
D[1, 2, 1, 2, 1]
Attempts:
2 left
💡 Hint
In strictly increasing prices, each day's price is higher than all previous days.
🧠 Conceptual
expert
1: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?
AO(n log n) because of sorting the prices before processing.
BO(n^2) because for each price, we may check all previous prices.
CO(log n) because the stack maintains sorted order for binary search.
DO(n) because each element is pushed and popped at most once from the stack.
Attempts:
2 left
💡 Hint
Consider how many times each price index is pushed and popped from the stack.