Bird
0
0
DSA Cprogramming~10 mins

Stock Span Problem Using Stack in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the stack array for storing indices.

DSA C
int stack[[1]];
Drag options to blanks, or click blank then click option'
A0
B10
C-1
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative size for the stack array.
Using too small size like 10 which may not hold all indices.
2fill in blank
medium

Complete the code to initialize the top of the stack.

DSA C
int top = [1];
Drag options to blanks, or click blank then click option'
A-1
B0
C1
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 which means stack is not empty.
Using a large number like 1000 which is out of bounds.
3fill in blank
hard

Fix the error in the while loop condition to pop prices less than or equal to the current price from the stack.

DSA C
while (top >= 0 && prices[stack[top]] [1] prices[i]) {
    top--;
}
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which does not pop equal prices.
Using '>=' or '>' which reverses the logic.
4fill in blank
hard

Fill both blanks to calculate the span correctly.

DSA C
span[i] = (top == -1) ? [1] : i - stack[2];
Drag options to blanks, or click blank then click option'
Ai + 1
Bi - 1
C[top]
D[top - 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 instead of i + 1 for empty stack case.
Using stack[top - 1] which may cause wrong index.
5fill in blank
hard

Fill all three blanks to push current index onto stack and print the spans.

DSA C
stack[++top] = [1];

for (int j = 0; j < n; j++) {
    printf("%d ", [2][[3]]);
}
Drag options to blanks, or click blank then click option'
Ai
Bspan
Cj
Dprices
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing wrong variable onto stack.
Printing prices instead of span.
Using wrong loop variable in print.