Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the stack size.
DSA C
stack->capacity = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting capacity to 0 or negative values causes no space allocation.
✗ Incorrect
The initial capacity of the dynamic stack is usually set to a positive number like 10 to allocate space.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication for doubling.
✗ Incorrect
Doubling capacity means multiplying the current capacity by 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than or less than instead of equality check.
✗ Incorrect
When top equals capacity, the stack is full and needs resizing.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top >= 0 allows empty stack access.
Using top index directly returns invalid element.
✗ Incorrect
We check if top is greater than 0 to ensure stack is not empty, then return element at top-1 index.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We allocate new array with capacity * sizeof(int), copy elements while i < top, then assign new_array to stack->array.
