Bird
0
0
DSA Cprogramming~10 mins

Dynamic Stack Using Resizable Array 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 stack size.

DSA C
stack->capacity = [1];
Drag options to blanks, or click blank then click option'
A10
B0
C-1
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Setting capacity to 0 or negative values causes no space allocation.
2fill in blank
medium

Complete the code to double the stack capacity when resizing.

DSA C
stack->capacity = stack->capacity [1] 2;
Drag options to blanks, or click blank then click option'
A/
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication for doubling.
3fill in blank
hard

Fix the error in the push function to check if resizing is needed.

DSA C
if (stack->top == stack->capacity [1]) {
    resize_stack(stack);
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than or less than instead of equality check.
4fill in blank
hard

Fill both blanks to correctly pop an element from the stack.

DSA C
if (stack->top [1] 0) {
    return stack->array[[2]];
} else {
    return -1; // stack empty
}
Drag options to blanks, or click blank then click option'
A>
B>=
Cstack->top - 1
Dstack->top
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top >= 0 allows empty stack access.
Using top index directly returns invalid element.
5fill in blank
hard

Fill all three blanks to correctly resize the stack array.

DSA C
int *new_array = malloc(stack->capacity [1] sizeof(int));
for (int i = 0; i [2] stack->top; i++) {
    new_array[i] = stack->array[i];
}
free(stack->array);
stack->array = [3];
Drag options to blanks, or click blank then click option'
A*
B<
Cnew_array
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication for malloc size.
Using i <= top causes out-of-bounds access.
Not assigning new_array back to stack->array.