Bird
0
0
DSA Cprogramming~20 mins

Stock Span Problem Using Stack in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stock Span Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Stock Span Calculation
What is the output of the following code that calculates the stock span for the given prices?
DSA C
int prices[] = {100, 80, 60, 70, 60, 75, 85};
int n = 7;
int span[7];
int stack[7];
int top = -1;

for (int i = 0; i < n; i++) {
    while (top >= 0 && prices[stack[top]] <= prices[i]) {
        top--;
    }
    span[i] = (top < 0) ? (i + 1) : (i - stack[top]);
    stack[++top] = i;
}

for (int i = 0; i < n; i++) {
    printf("%d ", span[i]);
}
A[1, 1, 2, 2, 3, 3, 4]
B[1, 2, 3, 4, 5, 6, 7]
C[1, 1, 1, 2, 1, 4, 6]
D[1, 1, 1, 1, 1, 1, 1]
Attempts:
2 left
💡 Hint
Think about how the stack helps track previous higher prices to calculate spans.
🧠 Conceptual
intermediate
1:30remaining
Understanding Stack Usage in Stock Span
Why do we use a stack to solve the Stock Span Problem efficiently?
ATo sort the stock prices in ascending order before calculating spans.
BTo keep track of indices of previous days with higher stock prices for quick span calculation.
CTo store the spans directly as they are calculated for each day.
DTo reverse the order of stock prices for backward traversal.
Attempts:
2 left
💡 Hint
Think about how the stack helps avoid repeated comparisons.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Stock Span Code
What error will the following code produce when calculating stock spans?
DSA C
int prices[] = {10, 4, 5, 90, 120, 80};
int n = 6;
int span[6];
int stack[6];
int top = 0;
stack[top] = 0;
span[0] = 1;

for (int i = 1; i < n; i++) {
    while (top >= 0 && prices[stack[top]] <= prices[i]) {
        top--;
    }
    span[i] = (top < 0) ? (i + 1) : (i - stack[top]);
    stack[++top] = i;
}

for (int i = 0; i < n; i++) {
    printf("%d ", span[i]);
}
ACompilation error due to uninitialized variables
BRuntime error due to stack underflow when top becomes -1 and then decremented again
COutputs incorrect spans: 1 1 1 1 1 1
DRuns correctly and outputs: 1 1 2 4 5 1
Attempts:
2 left
💡 Hint
Check how the stack top is initialized and updated before the loop.
Predict Output
advanced
2:00remaining
Output of Stock Span with Repeated Prices
What is the output of the stock span calculation for the prices array with repeated values?
DSA C
int prices[] = {30, 35, 35, 40, 38, 38, 42};
int n = 7;
int span[7];
int stack[7];
int top = -1;

for (int i = 0; i < n; i++) {
    while (top >= 0 && prices[stack[top]] <= prices[i]) {
        top--;
    }
    span[i] = (top < 0) ? (i + 1) : (i - stack[top]);
    stack[++top] = i;
}

for (int i = 0; i < n; i++) {
    printf("%d ", span[i]);
}
A[1, 2, 3, 4, 1, 2, 7]
B[1, 1, 1, 1, 1, 1, 1]
C[1, 2, 2, 4, 1, 2, 7]
D[1, 2, 1, 4, 1, 1, 7]
Attempts:
2 left
💡 Hint
Remember that equal prices are included in the span calculation as less or equal.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Stock Span Algorithm Using Stack
What is the time complexity of the stock span problem solution using a stack and why?
AO(n) because each element is pushed and popped at most once from the stack.
BO(n^2) because for each element we may compare with all previous elements.
CO(log n) because the stack operations are logarithmic in size.
DO(1) because the span is calculated in constant time for each element.
Attempts:
2 left
💡 Hint
Consider how many times each element can be pushed or popped from the stack.