Bird
0
0
DSA Cprogramming~10 mins

Min Stack Design 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 initialize the min stack's top pointer.

DSA C
typedef struct {
    int *stack;
    int *minStack;
    int top;
    int minTop;
} MinStack;

void initMinStack(MinStack *s) {
    s->top = [1];
    s->minTop = -1;
}
Drag options to blanks, or click blank then click option'
A-1
B0
C1
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 causes off-by-one errors.
Using NULL for an integer top index is invalid.
2fill in blank
medium

Complete the code to push a value onto the main stack.

DSA C
void push(MinStack *s, int val) {
    s->stack[++[1]] = val;
}
Drag options to blanks, or click blank then click option'
AminTop
Btop
Cstack
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using minTop instead of top pushes to the wrong stack.
Not incrementing top causes overwriting the same position.
3fill in blank
hard

Fix the error in the condition to update the min stack when pushing a new value.

DSA C
if (s->minTop == -1 || val [1] s->minStack[s->minTop]) {
    s->minStack[++s->minTop] = val;
}
Drag options to blanks, or click blank then click option'
A>=
B>
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes incorrect min tracking.
Using '==' misses smaller values.
4fill in blank
hard

Fill both blanks to correctly pop from the main stack and update the min stack if needed.

DSA C
int pop(MinStack *s) {
    int val = s->stack[s->[1]--];
    if (val == s->minStack[s->[2]]) {
        s->minTop--;
    }
    return val;
}
Drag options to blanks, or click blank then click option'
Atop
BminTop
Cstack
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up top and minTop indices.
Not decrementing top or minTop correctly.
5fill in blank
hard

Fill all three blanks to return the current minimum value from the min stack.

DSA C
int getMin(MinStack *s) {
    if (s->[1] == -1) {
        return [2]; // empty stack case
    }
    return s->minStack[s->[3]];
}
Drag options to blanks, or click blank then click option'
AminTop
B-1
Dtop
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top instead of minTop for empty.
Returning wrong index from minStack.