📖 Scenario: Imagine you are tracking the daily prices of a stock. You want to find out for each day how many consecutive days before it had a price less than or equal to that day's price. This helps investors understand the stock's recent performance trend.
🎯 Goal: Build a program that calculates the stock span for each day using a stack data structure.
📋 What You'll Learn
Create an array called prices with the exact values: 100, 80, 60, 70, 60, 75, 85
Create an integer variable called n and set it to the length of prices
Create an integer array called span of size n to store the span values
Use a stack implemented as an integer array called stack and an integer top to manage it
Calculate the stock span for each day using the stack
Print the span array values separated by spaces
💡 Why This Matters
🌍 Real World
Stock span calculation helps investors understand how many days a stock price has been rising or stable, which is useful for making buy or sell decisions.
💼 Career
Understanding stack data structures and their applications like the stock span problem is important for software developers working on financial software, trading platforms, and algorithmic problem solving.
Progress0 / 4 steps
1
Create the prices array and its size
Create an integer array called prices with these exact values: 100, 80, 60, 70, 60, 75, 85. Then create an integer variable called n and set it to 7.
DSA C
Hint
Use int prices[] = { ... }; to create the array and int n = 7; for the size.
2
Create the span array and stack variables
Create an integer array called span of size n. Also create an integer array called stack of size n and an integer variable called top initialized to -1.
DSA C
Hint
Use int span[7]; and int stack[7]; to create arrays and int top = -1; to start the stack empty.
3
Calculate the stock span using the stack
Write a for loop with variable i from 0 to n-1. Inside the loop, pop from the stack while top is not -1 and prices[stack[top]] is less than or equal to prices[i]. Then set span[i] to i + 1 if top is -1, else to i - stack[top]. Finally, push i onto the stack by incrementing top and setting stack[top] = i.
DSA C
Hint
Use a loop from 0 to n-1. Inside, pop from stack while top is not -1 and price at stack[top] is less or equal to current price. Then calculate span and push current index.
4
Print the span array
Use a for loop with variable i from 0 to n-1 to print each span[i] followed by a space.
DSA C
Hint
Use a loop to print each span value followed by a space using printf("%d ", span[i]);.
Practice
(1/5)
1. What does the Stock Span Problem calculate for each day in a list of stock prices?
easy
A. The maximum stock price in the entire list
B. The number of consecutive days before the current day with a price less than or equal to the current day's price
C. The difference between the highest and lowest stock price
D. The total sum of stock prices up to the current day
Solution
Step 1: Understand the problem definition
The Stock Span Problem finds how many consecutive previous days had stock prices less than or equal to the current day's price.
Step 2: Identify what is calculated for each day
For each day, it counts the number of consecutive days before it with prices not greater than the current day's price.
Final Answer:
The number of consecutive days with price less or equal to current day -> Option B
Quick Check:
Span counts consecutive smaller/equal prices [OK]
Hint: Span counts consecutive days with price ≤ current day price [OK]
Common Mistakes:
Confusing span with maximum price
Calculating total sum instead of span
Counting days with price greater than current
2. Which of the following C code snippets correctly initializes an empty stack for solving the Stock Span Problem?
easy
A. int stack[100]; int top = -1;
B. int stack[] = {0}; int top = 0;
C. int *stack; top = NULL;
D. stack = new int[100]; top = -1;
Solution
Step 1: Identify correct stack initialization in C
In C, stack is usually an array with an integer top initialized to -1 to indicate empty stack.
Step 2: Check each option
int stack[100]; int top = -1; correctly declares an int array and sets top to -1. int stack[] = {0}; int top = 0; initializes top to 0 which is incorrect for empty stack. int *stack; top = NULL; uses pointer and NULL incorrectly. stack = new int[100]; top = -1; uses 'new' which is C++ syntax, not C.
Final Answer:
int stack[100]; int top = -1; -> Option A
Quick Check:
Array + top = -1 initializes empty stack in C [OK]
Hint: Use array and top = -1 for empty stack in C [OK]
Common Mistakes:
Using C++ 'new' keyword in C code
Setting top to 0 instead of -1
Using uninitialized pointers for stack
3. What is the output span array for the stock prices [100, 80, 60, 70, 60, 75, 85] using the Stock Span algorithm?
Hint: Count consecutive previous days with price ≤ current day [OK]
Common Mistakes:
Counting all previous days instead of consecutive
Ignoring equal prices in span
Miscounting span for days with fluctuating prices
4. Identify the error in the following C code snippet for Stock Span calculation:
for (int i = 0; i < n; i++) {
while (top != -1 && prices[stack[top]] <= prices[i]) {
top--;
}
span[i] = (top == -1) ? (i + 1) : (i - stack[top]);
stack[++top] = i;
}
medium
A. The condition 'top != -1' should be 'top == -1'
B. The stack push operation should be before calculating span
C. No error; code correctly calculates the stock span
D. The comparison operator in while loop should be '<' instead of '<='
Solution
Step 1: Analyze the while loop condition
The while loop pops indices from stack while the price at those indices is less than or equal to current price, which is correct for span calculation.
Step 2: Check span calculation and stack push
Span is set to i+1 if stack empty, else difference between current index and top of stack index. Then current index is pushed. This matches the standard algorithm.
Final Answer:
No error; code correctly calculates the stock span -> Option C
Quick Check:
Standard stack-based span algorithm used [OK]
Hint: Check while condition and span formula carefully [OK]
Common Mistakes:
Changing <= to < breaks span logic
Pushing before span calculation causes wrong results
Incorrect stack empty condition
5. Given stock prices [10, 4, 5, 90, 120, 80], how would the Stock Span algorithm change if we want to count days with strictly less price (not less or equal)? What is the resulting span array?
hard
A. [1, 1, 2, 4, 5, 2]
B. [1, 1, 1, 4, 5, 1]
C. [1, 1, 1, 3, 4, 1]
D. [1, 1, 2, 4, 5, 1]
Solution
Step 1: Understand change from ≤ to < in comparison
Changing from less or equal to strictly less means while popping from stack, we pop only if previous price is strictly less than current price, not equal.
Step 2: Calculate span with strict less condition
Day 0: 10, span=1 Day 1: 4, previous 10 > 4, span=1 Day 2: 5, previous 4 < 5, pop 4, span=2 Day 3: 90, previous 5 < 90, pop 5; previous 10 < 90, pop 10; stack empty, span=4 Day 4: 120, previous 90 < 120, pop 90; stack empty, span=5 Day 5: 80, previous 120 > 80, span=1 (no pop) But since we pop only if strictly less, equal prices would not pop. Here no equal prices affect.
Final Answer:
[1, 1, 2, 4, 5, 1] -> Option D
Quick Check:
Strict less changes popping condition, span updated accordingly [OK]
Hint: Use '<' instead of '<=' in while condition for strict less [OK]
Common Mistakes:
Not updating while condition correctly
Miscounting span for last day
Assuming equal prices pop stack
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.