Bird
0
0
DSA Cprogramming~20 mins

Dynamic Stack Using Resizable Array in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A10 -> 20 -> 30 -> 40 -> null
B30 -> 40 -> 20 -> 10 -> null
C40 -> 20 -> 30 -> 10 -> null
D40 -> 30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that stack is Last In First Out (LIFO). The last pushed element is on top.
Predict Output
intermediate
2: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);
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Count pushes and pops carefully.
🔧 Debug
advanced
2: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;
}
APossible memory leak or invalid memory access due to realloc without checking NULL
BStack overflow error
CNo error, works correctly
DDivision by zero error
Attempts:
2 left
💡 Hint
Check how realloc is used and what happens if it fails.
🧠 Conceptual
advanced
2: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?
ADoubling capacity prevents stack underflow
BDoubling capacity wastes less memory than fixed increments
CDoubling capacity reduces the number of resizes, improving amortized time complexity
DDoubling capacity is easier to implement than fixed increments
Attempts:
2 left
💡 Hint
Think about how often resizing happens and the cost of copying elements.
🚀 Application
expert
3: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);
A4 -> 1 -> null
B5 -> 4 -> 1 -> null
C2 -> 1 -> null
D1 -> 4 -> null
Attempts:
2 left
💡 Hint
Track each push and pop carefully, remembering LIFO order.