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 C# code that calculates the stock span for the given prices array?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] prices = {100, 80, 60, 70, 60, 75, 85}; int n = prices.Length; int[] span = new int[n]; Stack<int> stack = new Stack<int>(); for (int i = 0; i < n; i++) { while (stack.Count > 0 && prices[stack.Peek()] <= prices[i]) { stack.Pop(); } span[i] = (stack.Count == 0) ? (i + 1) : (i - stack.Peek()); stack.Push(i); } for (int i = 0; i < n; i++) { Console.Write(span[i] + " "); } } }
Attempts:
2 left
💡 Hint
Remember the stack stores indices of days with higher stock prices to calculate span.
✗ Incorrect
The stock span for each day is the number of consecutive days before it with a price less than or equal to the current day's price. The stack helps track indices of days with higher prices to calculate this efficiently.
🧠 Conceptual
intermediateUnderstanding 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 find the previous greater element quickly.
✗ Incorrect
The stack stores indices of days with higher prices so that when we process a new day, we can quickly find how many consecutive previous days had lower or equal prices by popping smaller or equal prices.
🔧 Debug
advancedIdentify the Error in Stock Span Code
What error will this C# code produce when calculating stock span?
DSA C#
int[] prices = {100, 80, 60, 70, 60, 75, 85}; int n = prices.Length; int[] span = new int[n]; Stack<int> stack = new Stack<int>(); for (int i = 0; i <= n; i++) { while (stack.Count > 0 && prices[stack.Peek()] <= prices[i]) { stack.Pop(); } span[i] = (stack.Count == 0) ? (i + 1) : (i - stack.Peek()); stack.Push(i); }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The for loop runs with i <= n, which tries to access prices[n], an invalid index causing IndexOutOfRangeException.
❓ Predict Output
advancedOutput of Stock Span with Different Prices
What is the output of the stock span calculation for the prices array {10, 4, 5, 90, 120, 80} using the standard stack approach?
DSA C#
int[] prices = {10, 4, 5, 90, 120, 80}; int n = prices.Length; int[] span = new int[n]; Stack<int> stack = new Stack<int>(); for (int i = 0; i < n; i++) { while (stack.Count > 0 && prices[stack.Peek()] <= prices[i]) { stack.Pop(); } span[i] = (stack.Count == 0) ? (i + 1) : (i - stack.Peek()); stack.Push(i); } for (int i = 0; i < n; i++) { Console.Write(span[i] + " "); }
Attempts:
2 left
💡 Hint
Check how many consecutive previous days have prices less or equal to current day.
✗ Incorrect
The span array is [1, 1, 2, 4, 5, 1] because for example, price 90 has 4 consecutive days before it with prices less or equal (10,4,5,90).
🧠 Conceptual
expertTime Complexity of Stock Span Algorithm
What is the time complexity of the stock span problem solution using a stack for n days of stock prices?
Attempts:
2 left
💡 Hint
Each element is pushed and popped at most once from the stack.
✗ Incorrect
The algorithm runs in O(n) time because each price index is pushed and popped at most once, making the total operations linear in n.
