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 Rust code that calculates the stock span for the given prices?
DSA Rust
fn calculate_span(prices: &[i32]) -> Vec<i32> {
let mut stack: Vec<usize> = Vec::new();
let mut spans = vec![0; prices.len()];
for i in 0..prices.len() {
while !stack.is_empty() && prices[*stack.last().unwrap()] <= prices[i] {
stack.pop();
}
spans[i] = if stack.is_empty() { (i + 1) as i32 } else { (i - *stack.last().unwrap()) as i32 };
stack.push(i);
}
spans
}
fn main() {
let prices = vec![100, 80, 60, 70, 60, 75, 85];
let result = calculate_span(&prices);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember, the span for a day is the count of consecutive days before it with price less or equal to current price.
✗ Incorrect
The stock span for each day is calculated by checking how many consecutive previous days had prices less than or equal to the current day's price. Using a stack to keep track of indices of days with higher prices helps efficiently compute spans.
🧠 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 to quickly find the previous day with a higher price.
✗ Incorrect
The stack stores indices of days with prices greater than the current day. This helps quickly find the nearest previous day with a higher price, which is essential to calculate the span efficiently.
🔧 Debug
advancedIdentify the Error in Stock Span Code
What error will the following Rust code produce when calculating stock spans?
DSA Rust
fn calculate_span(prices: &[i32]) -> Vec<i32> {
let mut stack: Vec<usize> = Vec::new();
let mut spans = vec![0; prices.len()];
for i in 0..prices.len() {
while !stack.is_empty() && prices[*stack.last().unwrap()] < prices[i] {
stack.pop();
}
spans[i] = if stack.is_empty() { (i + 1) as i32 } else { (i - *stack.last().unwrap()) as i32 };
stack.push(i);
}
spans
}
fn main() {
let prices = vec![100, 80, 60, 70, 60, 75, 85];
let result = calculate_span(&prices);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check the comparison operator in the while loop condition.
✗ Incorrect
Using '<' instead of '<=' causes the code to ignore days with equal prices, resulting in incorrect span values.
❓ Predict Output
advancedOutput of Stock Span for All Increasing Prices
What is the output of the stock span function for strictly increasing prices?
DSA Rust
fn calculate_span(prices: &[i32]) -> Vec<i32> {
let mut stack: Vec<usize> = Vec::new();
let mut spans = vec![0; prices.len()];
for i in 0..prices.len() {
while !stack.is_empty() && prices[*stack.last().unwrap()] <= prices[i] {
stack.pop();
}
spans[i] = if stack.is_empty() { (i + 1) as i32 } else { (i - *stack.last().unwrap()) as i32 };
stack.push(i);
}
spans
}
fn main() {
let prices = vec![10, 20, 30, 40, 50];
let result = calculate_span(&prices);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
In strictly increasing prices, each day's price is higher than all previous days.
✗ Incorrect
Since each price is higher than all previous prices, the span for day i is i+1, counting all previous days.
🧠 Conceptual
expertTime Complexity of Stock Span Algorithm Using Stack
What is the time complexity of the stock span algorithm implemented using a stack for n days of prices?
Attempts:
2 left
💡 Hint
Consider how many times each price index is pushed and popped from the stack.
✗ Incorrect
Each price index is pushed once and popped once from the stack, so total operations are proportional to n, making the algorithm O(n).
