Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of direct array manipulation vs stack abstraction
Consider the following C code snippets. What is the printed output after executing each snippet?
DSA C
/* Snippet 1: Direct array use */ #include <stdio.h> int main() { int arr[3]; int top = -1; arr[++top] = 10; arr[++top] = 20; arr[++top] = 30; printf("%d %d %d\n", arr[0], arr[1], arr[2]); return 0; } /* Snippet 2: Using stack abstraction */ #include <stdio.h> #include <stdlib.h> typedef struct { int *data; int top; int capacity; } Stack; void push(Stack *s, int val) { if (s->top == s->capacity - 1) return; s->data[++s->top] = val; } int main() { Stack s; s.capacity = 3; s.top = -1; s.data = (int *)malloc(s.capacity * sizeof(int)); push(&s, 10); push(&s, 20); push(&s, 30); printf("%d %d %d\n", s.data[0], s.data[1], s.data[2]); free(s.data); return 0; }
Attempts:
2 left
💡 Hint
Think about how elements are added and accessed in both direct array and stack abstraction.
✗ Incorrect
Both snippets add elements in the same order and print them in the same order. The stack abstraction hides the details but stores elements similarly.
🧠 Conceptual
intermediate1:30remaining
Why use stack abstraction instead of direct array use?
Which of the following is the main reason to use a stack abstraction instead of directly manipulating an array?
Attempts:
2 left
💡 Hint
Think about safety and controlled access.
✗ Incorrect
Stack abstraction controls how elements are added or removed, preventing errors like popping when empty.
🔧 Debug
advanced2:00remaining
Identify the error in direct array stack implementation
What error will occur when running this code snippet that uses an array as a stack without abstraction?
DSA C
#include <stdio.h> int main() { int arr[2]; int top = -1; arr[++top] = 5; arr[++top] = 10; arr[++top] = 15; // Problem here printf("%d %d %d\n", arr[0], arr[1], arr[2]); return 0; }
Attempts:
2 left
💡 Hint
Check the array size and how many elements are inserted.
✗ Incorrect
The array size is 2 but code tries to insert 3 elements, causing out of bounds access at runtime.
❓ Predict Output
advanced2:00remaining
Output after stack pop operation using abstraction
What is the output of this C code that uses stack abstraction and performs push and pop?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct { int *data; int top; int capacity; } Stack; void push(Stack *s, int val) { if (s->top == s->capacity - 1) return; s->data[++s->top] = val; } int pop(Stack *s) { if (s->top == -1) return -1; return s->data[s->top--]; } int main() { Stack s; s.capacity = 3; s.top = -1; s.data = (int *)malloc(s.capacity * sizeof(int)); push(&s, 100); push(&s, 200); push(&s, 300); int val = pop(&s); printf("%d %d %d\n", s.data[0], s.data[1], s.data[2]); printf("Popped: %d\n", val); free(s.data); return 0; }
Attempts:
2 left
💡 Hint
Pop returns the last pushed element but does not clear the array slot.
✗ Incorrect
Pop returns 300 but the array still holds 300 at index 2 because pop only moves the top pointer.
🧠 Conceptual
expert2:30remaining
Why stack abstraction improves code safety and maintainability
Which statement best explains why using a stack abstraction is better than direct array manipulation for stack operations?
Attempts:
2 left
💡 Hint
Think about abstraction benefits in programming.
✗ Incorrect
Stack abstraction hides how data is stored and controls operations, reducing errors and improving maintainability.
