Challenge - 5 Problems
Stack Array 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 stack push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. The following operations are performed in order:
push(10), push(20), pop(), push(30), push(40), pop(), pop()
What is the printed output of the popped elements?
push(10), push(20), pop(), push(30), push(40), pop(), pop()
What is the printed output of the popped elements?
DSA C
int stack[3]; int top = -1; void push(int x) { if (top == 2) return; // stack full stack[++top] = x; } int pop() { if (top == -1) return -1; // stack empty return stack[top--]; } int main() { push(10); push(20); printf("%d ", pop()); push(30); push(40); printf("%d ", pop()); printf("%d", pop()); return 0; }
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and returns it.
✗ Incorrect
The first pop removes 20 (last pushed), then push 30 and 40 fills the stack. Next pop removes 40, then pop removes 30.
❓ Predict Output
intermediate2:00remaining
What is the value of 'top' after these operations?
Given a stack implemented with an array of size 5, initially empty (top = -1). The operations are:
push(5), push(10), push(15), pop(), push(20), pop(), pop()
What is the value of 'top' after these operations?
push(5), push(10), push(15), pop(), push(20), pop(), pop()
What is the value of 'top' after these operations?
DSA C
int stack[5]; int top = -1; void push(int x) { if (top == 4) return; stack[++top] = x; } void pop() { if (top == -1) return; top--; } int main() { push(5); push(10); push(15); pop(); push(20); pop(); pop(); printf("%d", top); return 0; }
Attempts:
2 left
💡 Hint
Track each push and pop carefully to update 'top'.
✗ Incorrect
After push(5), top=0; push(10), top=1; push(15), top=2; pop(), top=1; push(20), top=2; pop(), top=1; pop(), top=0.
🔧 Debug
advanced2:00remaining
What error does this stack push function cause?
Identify the error in this push function for a stack implemented using an array of size 3:
void push(int x) { if (top == 3) { printf("Stack Overflow\n"); return; } stack[++top] = x; }
Assume 'top' is initialized to -1.
void push(int x) { if (top == 3) { printf("Stack Overflow\n"); return; } stack[++top] = x; }
Assume 'top' is initialized to -1.
DSA C
int stack[3]; int top = -1; void push(int x) { if (top == 3) { printf("Stack Overflow\n"); return; } stack[++top] = x; }
Attempts:
2 left
💡 Hint
Array indices go from 0 to size-1.
✗ Incorrect
The condition should be 'top == 2' for size 3, because valid indices are 0,1,2. Checking 'top == 3' allows writing beyond array bounds.
❓ Predict Output
advanced2:00remaining
What is the output after these stack operations?
Given a stack of size 4, initially empty, perform:
push(1), push(2), push(3), pop(), push(4), push(5), pop(), pop(), pop()
Print the popped values in order separated by spaces.
push(1), push(2), push(3), pop(), push(4), push(5), pop(), pop(), pop()
Print the popped values in order separated by spaces.
DSA C
int stack[4]; int top = -1; void push(int x) { if (top == 3) return; stack[++top] = x; } int pop() { if (top == -1) return -1; return stack[top--]; } int main() { push(1); push(2); push(3); printf("%d ", pop()); push(4); push(5); printf("%d ", pop()); printf("%d ", pop()); printf("%d", pop()); return 0; }
Attempts:
2 left
💡 Hint
Remember push fails silently if stack is full.
✗ Incorrect
Pop 1 removes 3, push 4 succeeds, push 5 succeeds (stack full now). Pop removes 5, pop removes 4, pop removes 2.
🧠 Conceptual
expert2:00remaining
Which option correctly describes the time complexity of stack operations using an array?
Consider a stack implemented using an array with fixed size. What is the time complexity of push and pop operations?
Attempts:
2 left
💡 Hint
Think about how many elements need to be moved during push or pop.
✗ Incorrect
Push and pop only change the 'top' index and assign or read one element, so both are constant time O(1).
