Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 causes off-by-one errors.
Using NULL for an integer top index is invalid.
✗ Incorrect
The top index should start at -1 to indicate the stack is empty.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using minTop instead of top pushes to the wrong stack.
Not incrementing top causes overwriting the same position.
✗ Incorrect
Incrementing top before assignment places the value correctly on the stack.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes incorrect min tracking.
Using '==' misses smaller values.
✗ Incorrect
We push to minStack if val is less than or equal to current min, so use '<='.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up top and minTop indices.
Not decrementing top or minTop correctly.
✗ Incorrect
Pop from main stack using top, and compare with minStack at minTop.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top instead of minTop for empty.
Returning wrong index from minStack.
✗ Incorrect
Check if minTop is -1 (empty), return -1 as error, else return minStack[minTop].
