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 stock span array after running the given code with prices [100, 80, 60, 70, 60, 75, 85]?
DSA Javascript
function calculateSpan(prices) {
const n = prices.length;
const span = new Array(n).fill(1);
const stack = [];
for (let i = 0; i < n; 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;
}
console.log(calculateSpan([100, 80, 60, 70, 60, 75, 85]));Attempts:
2 left
💡 Hint
Remember the span counts how many consecutive days before the current day had prices less than or equal to the current price.
✗ Incorrect
The stock span array shows how many consecutive previous days had prices less than or equal to the current day's price. The stack helps track indices of days with higher prices to calculate spans 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 nearest previous day with a higher price.
✗ Incorrect
The stack stores indices of days with prices higher than the current day, allowing us to quickly find the last day with a higher price and calculate the span without checking all previous days.
🔧 Debug
advancedIdentify the Error in Stock Span Code
What error will the following code produce when calculating stock span for prices [10, 4, 5, 90, 120, 80]?
function calculateSpan(prices) {
const span = [];
const stack = [];
for (let i = 0; i <= prices.length; i++) {
while (stack.length && 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;
}
console.log(calculateSpan([10, 4, 5, 90, 120, 80]));
Attempts:
2 left
💡 Hint
Check the loop boundary and array access inside the loop.
✗ Incorrect
The loop runs until i <= prices.length, which causes prices[i] to be undefined on the last iteration, leading to a TypeError when comparing undefined with a number.
❓ Predict Output
advancedOutput of Modified Stock Span with Equal Prices
What is the output of the stock span array for prices [30, 35, 35, 40, 38, 38, 42] using the standard stack-based algorithm?
DSA Javascript
function calculateSpan(prices) {
const n = prices.length;
const span = new Array(n).fill(1);
const stack = [];
for (let i = 0; i < n; 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;
}
console.log(calculateSpan([30, 35, 35, 40, 38, 38, 42]));Attempts:
2 left
💡 Hint
Equal prices are included in the span count because the condition uses <=.
✗ Incorrect
The algorithm counts consecutive previous days with prices less than or equal to current price. Equal prices increase the span count.
🚀 Application
expertMaximum Stock Span Value
Given the prices array [31, 41, 48, 59, 79, 79, 79, 60, 55, 70], what is the maximum value in the stock span array computed by the standard stack-based algorithm?
DSA Javascript
function calculateSpan(prices) {
const n = prices.length;
const span = new Array(n).fill(1);
const stack = [];
for (let i = 0; i < n; 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;
}
const prices = [31, 41, 48, 59, 79, 79, 79, 60, 55, 70];
const spans = calculateSpan(prices);
console.log(Math.max(...spans));Attempts:
2 left
💡 Hint
Calculate spans carefully, especially for repeated equal prices.
✗ Incorrect
The maximum span is 7, occurring at the last 79 price, counting all previous days with prices less or equal.
