Challenge - 5 Problems
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this dynamic stack push sequence?
Consider a dynamic stack implemented using a resizable array starting with capacity 2. After pushing elements 10, 20, 30, and 40 in order, what is the content of the stack from top to bottom?
DSA C
DynamicStack stack; initStack(&stack); push(&stack, 10); push(&stack, 20); push(&stack, 30); push(&stack, 40); printStack(&stack);
Attempts:
2 left
💡 Hint
Remember that stack is Last In First Out (LIFO). The last pushed element is on top.
✗ Incorrect
The stack pushes elements in order: 10, 20, 30, 40. The top is the last pushed element, 40. So the stack from top to bottom is 40, 30, 20, 10.
❓ Predict Output
intermediate2:00remaining
What is the stack size after these operations?
Given a dynamic stack with initial capacity 2, perform these operations: push 5, push 15, push 25, pop, push 35. What is the size of the stack after these operations?
DSA C
DynamicStack stack; initStack(&stack); push(&stack, 5); push(&stack, 15); push(&stack, 25); pop(&stack); push(&stack, 35); int size = stackSize(&stack); printf("%d", size);
Attempts:
2 left
💡 Hint
Count pushes and pops carefully.
✗ Incorrect
Push 5, 15, 25 → size 3. Pop removes top (25) → size 2. Push 35 → size 3.
🔧 Debug
advanced2:00remaining
Identify the error in this dynamic stack pop function
What error will occur when running this pop function on a dynamic stack implemented with a resizable array?
DSA C
int pop(DynamicStack *stack) { if (stack->top == -1) { printf("Stack Underflow\n"); return -1; } int value = stack->array[stack->top]; stack->top--; if (stack->top < stack->capacity / 4) { stack->capacity /= 2; stack->array = realloc(stack->array, stack->capacity * sizeof(int)); } return value; }
Attempts:
2 left
💡 Hint
Check how realloc is used and what happens if it fails.
✗ Incorrect
If realloc fails, it returns NULL but the original memory is not freed. Assigning directly to stack->array without checking causes memory leak or invalid access.
🧠 Conceptual
advanced2:00remaining
Why does the dynamic stack double its capacity when full?
In a dynamic stack implemented with a resizable array, why is the capacity doubled when the stack is full instead of increasing by a fixed amount?
Attempts:
2 left
💡 Hint
Think about how often resizing happens and the cost of copying elements.
✗ Incorrect
Doubling capacity reduces the frequency of resizing, so push operations have amortized O(1) time. Fixed increments cause frequent resizing and higher total cost.
🚀 Application
expert3:00remaining
What is the output after this sequence of push and pop operations?
Given a dynamic stack with initial capacity 2, perform these operations in order: push 1, push 2, push 3, pop, pop, push 4, push 5, pop. What is the content of the stack from top to bottom?
DSA C
DynamicStack stack; initStack(&stack); push(&stack, 1); push(&stack, 2); push(&stack, 3); pop(&stack); pop(&stack); push(&stack, 4); push(&stack, 5); pop(&stack); printStack(&stack);
Attempts:
2 left
💡 Hint
Track each push and pop carefully, remembering LIFO order.
✗ Incorrect
Push 1,2,3 → stack top:3,2,1. Pop removes 3, then pop removes 2 → stack top:1. Push 4,5 → stack top:5,4,1. Pop removes 5 → stack top:4,1.
