Challenge - 5 Problems
Stock Span Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Stock Span Calculation
What is the output of the following Java code that calculates the stock span for given prices?
DSA Java
import java.util.*; public class StockSpan { public static int[] calculateSpan(int[] prices) { int n = prices.length; int[] span = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && prices[stack.peek()] <= prices[i]) { stack.pop(); } span[i] = stack.isEmpty() ? (i + 1) : (i - stack.peek()); stack.push(i); } return span; } public static void main(String[] args) { int[] prices = {100, 80, 60, 70, 60, 75, 85}; int[] result = calculateSpan(prices); for (int val : result) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Think about how the stack helps track previous higher prices to calculate spans.
✗ Incorrect
The stack stores indices of days with higher prices. For each day, span is the difference between current day and last higher price day. If none, span is current day index + 1.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Stock Span
Why is a stack used in the stock span problem instead of a simple loop checking all previous days?
Attempts:
2 left
💡 Hint
Consider how many times each element is pushed and popped in the stack.
✗ Incorrect
Stack helps skip unnecessary comparisons by storing indices of days with higher prices, so each element is pushed and popped at most once, making the algorithm O(n).
🔧 Debug
advancedIdentify the Error in Stock Span Code
What error will the following code produce when run?
DSA Java
import java.util.*; public class StockSpan { public static int[] calculateSpan(int[] prices) { int n = prices.length; int[] span = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i <= n; i++) { while (!stack.isEmpty() && prices[stack.peek()] <= prices[i]) { stack.pop(); } span[i] = stack.isEmpty() ? (i + 1) : (i - stack.peek()); stack.push(i); } return span; } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs from i = 0 to i <= n, which causes i to reach n, an invalid index for the prices array, causing ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedOutput of Stock Span with All Increasing Prices
What is the output of the stock span calculation for strictly increasing prices?
DSA Java
import java.util.*; public class StockSpan { public static int[] calculateSpan(int[] prices) { int n = prices.length; int[] span = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && prices[stack.peek()] <= prices[i]) { stack.pop(); } span[i] = stack.isEmpty() ? (i + 1) : (i - stack.peek()); stack.push(i); } return span; } public static void main(String[] args) { int[] prices = {10, 20, 30, 40, 50}; int[] result = calculateSpan(prices); for (int val : result) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
In increasing prices, each price is higher than all previous, so span grows.
✗ Incorrect
Each new price is higher than all before, so span equals current day index + 1.
🧠 Conceptual
expertTime Complexity Analysis of Stock Span Algorithm
What is the time complexity of the stock span algorithm using a stack and why?
Attempts:
2 left
💡 Hint
Consider how many times each index is pushed and popped.
✗ Incorrect
Each price index is pushed once and popped once, so total operations are proportional to n, making it O(n).
