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 given prices?
DSA C++
#include <iostream> #include <stack> #include <vector> using namespace std; vector<int> calculateSpan(const vector<int>& prices) { vector<int> span(prices.size()); stack<int> s; // stores indices for (int i = 0; i < prices.size(); i++) { while (!s.empty() && prices[s.top()] <= prices[i]) { s.pop(); } span[i] = s.empty() ? (i + 1) : (i - s.top()); s.push(i); } return span; } int main() { vector<int> prices = {100, 80, 60, 70, 60, 75, 85}; vector<int> result = calculateSpan(prices); for (int val : result) { cout << val << " "; } return 0; }
Attempts:
2 left
💡 Hint
Think about how the stack helps find the previous higher price index.
✗ Incorrect
The stock span for each day is the count of consecutive days before it with price less or equal. The stack stores 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 greater element to the left.
✗ Incorrect
The stack stores indices of days with prices greater than the current day's price, allowing quick calculation of how many consecutive days had lower or equal prices.
🔧 Debug
advancedIdentify the Bug in Stock Span Code
What error will the following code produce when calculating stock span?
DSA C++
#include <iostream> #include <stack> #include <vector> using namespace std; vector<int> calculateSpan(const vector<int>& prices) { vector<int> span(prices.size()); stack<int> s; for (int i = 0; i <= prices.size(); i++) { while (!s.empty() && prices[s.top()] <= prices[i]) { s.pop(); } span[i] = s.empty() ? (i + 1) : (i - s.top()); s.push(i); } return span; } int main() { vector<int> prices = {100, 80, 60, 70, 60, 75, 85}; vector<int> result = calculateSpan(prices); for (int val : result) { cout << val << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs with i <= prices.size(), which causes access to prices[7] when prices size is 7, leading to out_of_range runtime error.
🚀 Application
advancedStock Span for Large Input
Given a large array of stock prices, which approach is best to calculate the stock span efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity for large inputs.
✗ Incorrect
Using a stack allows O(n) time complexity by avoiding repeated comparisons, unlike nested loops which are O(n^2). Sorting changes order and is not suitable.
❓ Predict Output
expertOutput of Modified Stock Span Code with Equal Prices
What is the output of this modified stock span code where prices can be equal?
DSA C++
#include <iostream> #include <stack> #include <vector> using namespace std; vector<int> calculateSpan(const vector<int>& prices) { vector<int> span(prices.size()); stack<int> s; for (int i = 0; i < prices.size(); i++) { while (!s.empty() && prices[s.top()] < prices[i]) { s.pop(); } span[i] = s.empty() ? (i + 1) : (i - s.top()); s.push(i); } return span; } int main() { vector<int> prices = {100, 100, 60, 70, 70, 75, 85}; vector<int> result = calculateSpan(prices); for (int val : result) { cout << val << " "; } return 0; }
Attempts:
2 left
💡 Hint
Note the difference in comparison operator in while loop.
✗ Incorrect
The code uses '<' instead of '<=' so equal prices are counted as part of the span, increasing span counts for equal consecutive prices.
