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 for Given Prices
What is the output of the stock span array after running the given Go code with prices [100, 80, 60, 70, 60, 75, 85]?
DSA Go
package main import "fmt" type Stack []int func (s *Stack) Push(v int) { *s = append(*s, v) } func (s *Stack) Pop() int { if len(*s) == 0 { return -1 } index := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] return index } func (s *Stack) Top() int { if len(*s) == 0 { return -1 } return (*s)[len(*s)-1] } func calculateSpan(prices []int) []int { n := len(prices) span := make([]int, n) var s Stack for i := 0; i < n; i++ { for len(s) > 0 && prices[s.Top()] <= prices[i] { s.Pop() } if len(s) == 0 { span[i] = i + 1 } else { span[i] = i - s.Top() } s.Push(i) } return span } func main() { prices := []int{100, 80, 60, 70, 60, 75, 85} span := calculateSpan(prices) fmt.Println(span) }
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 for each day is calculated by counting how many consecutive previous days had prices less than or equal to the current day's price. Using a stack to track indices of days with higher prices helps efficiently compute this.
🧠 Conceptual
intermediateUnderstanding Stack Usage in Stock Span Problem
Why is a stack used in the stock span problem instead of a simple loop checking all previous days?
Attempts:
2 left
💡 Hint
Think about how to avoid checking all previous days repeatedly.
✗ Incorrect
The stack stores indices of days with prices higher than the current day, allowing us to jump directly to the last higher price day and avoid repeated comparisons, achieving O(n) time complexity.
🔧 Debug
advancedIdentify the Error in Stock Span Stack Implementation
What error will the following Go code produce when calculating stock span for prices [10, 4, 5, 90, 120, 80]?
func calculateSpan(prices []int) []int {
n := len(prices)
span := make([]int, n)
var s Stack
for i := 0; i < n; i++ {
for len(s) > 0 && prices[s.Top()] < prices[i] {
s.Pop()
}
if len(s) == 0 {
span[i] = i
} else {
span[i] = i - s.Top()
}
s.Push(i)
}
return span
}
Attempts:
2 left
💡 Hint
Check the span assignment when the stack is empty.
✗ Incorrect
The span for the first day should be 1 (the day itself), but the code assigns i (which is 0), so the span is zero, which is incorrect.
🚀 Application
advancedApplying Stock Span Logic to Find Maximum Span Day
Given the prices array [31, 27, 14, 21, 30, 22, 35], which day (0-based index) has the maximum stock span?
Attempts:
2 left
💡 Hint
Calculate the span array first, then find the index with the largest span value.
✗ Incorrect
The maximum span corresponds to the longest consecutive days with prices less or equal to the current day. Day 6 has the highest price and the longest span.
❓ Predict Output
expertOutput of Stock Span with Repeated Prices
What is the output of the stock span array for prices [10, 10, 10, 10, 10] using the standard stack-based algorithm?
DSA Go
package main import "fmt" type Stack []int func (s *Stack) Push(v int) { *s = append(*s, v) } func (s *Stack) Pop() int { if len(*s) == 0 { return -1 } index := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] return index } func (s *Stack) Top() int { if len(*s) == 0 { return -1 } return (*s)[len(*s)-1] } func calculateSpan(prices []int) []int { n := len(prices) span := make([]int, n) var s Stack for i := 0; i < n; i++ { for len(s) > 0 && prices[s.Top()] <= prices[i] { s.Pop() } if len(s) == 0 { span[i] = i + 1 } else { span[i] = i - s.Top() } s.Push(i) } return span } func main() { prices := []int{10, 10, 10, 10, 10} span := calculateSpan(prices) fmt.Println(span) }
Attempts:
2 left
💡 Hint
Repeated equal prices increase the span count cumulatively.
✗ Incorrect
Since all prices are equal, each day’s span includes all previous days plus itself, resulting in increasing spans from 1 to 5.
