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 TypeScript code that calculates the stock span for given prices?
DSA Typescript
function calculateSpan(prices: number[]): number[] {
const stack: number[] = [];
const span: number[] = new Array(prices.length).fill(1);
for (let i = 0; i < prices.length; 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 = [100, 80, 60, 70, 60, 75, 85];
console.log(calculateSpan(prices));Attempts:
2 left
š” Hint
Think about how the stack helps track previous higher prices to calculate the span.
ā Incorrect
The code uses a stack to keep indices of days with higher prices. For each day, it pops all days with prices less or equal to current, then calculates span as distance from last higher price day or from start if none.
š§ Conceptual
intermediateUnderstanding Stack Usage in Stock Span
Why is a stack used in the stock span problem instead of a simple loop checking previous days?
Attempts:
2 left
š” Hint
Consider how many times each price is pushed and popped in the stack.
ā Incorrect
Stack helps skip checking all previous days by popping smaller or equal prices, so each element is pushed and popped once, making the algorithm linear.
š§ Debug
advancedIdentify the Error in Stock Span Code
What error does the following TypeScript code produce when calculating stock span?
DSA Typescript
function calculateSpan(prices: number[]): number[] {
const stack: number[] = [];
const span: number[] = new Array(prices.length).fill(1);
for (let i = 0; i < prices.length; i++) {
while (stack.length > 0 && prices[stack[stack.length]] <= 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
Check the index used to access the top element of the stack inside the while loop condition.
ā Incorrect
The code uses stack[stack.length] which is undefined because array indices go from 0 to length-1. Accessing stack[stack.length] returns undefined, then prices[undefined] is undefined, and undefined <= prices[i] is false, so the while never pops, producing incorrect output [1,1,1,1,1,1,1].
š Application
advancedCalculate Stock Span for New Prices
Given the prices array [31, 41, 48, 59, 79], what is the stock span array produced by the stack-based algorithm?
Attempts:
2 left
š” Hint
Prices are strictly increasing, so each day's span includes all previous days.
ā Incorrect
Since each price is higher than all previous, the span increases by 1 each day, resulting in [1, 2, 3, 4, 5].
ā Predict Output
expertOutput of Modified Stock Span with Equal Prices
What is the output of this modified TypeScript code that treats equal prices as breaking the span?
DSA Typescript
function calculateSpanModified(prices: number[]): number[] {
const stack: number[] = [];
const span: number[] = new Array(prices.length).fill(1);
for (let i = 0; i < prices.length; 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 = [100, 80, 80, 70, 60, 75, 85];
console.log(calculateSpanModified(prices));Attempts:
2 left
š” Hint
Note the condition changed from <= to < in the while loop, so equal prices stop popping.
ā Incorrect
Because equal prices do not cause popping, the span resets at repeated prices, resulting in spans of 1 for repeated 80s and [1,1,1,1,1,3,6] overall.
