Initially stack = []. After push(10), stack = [10]. After push(20), stack = [10, 20]. After pop(), top element 20 is removed, stack = [10]. After push(30), stack = [10, 30].
Step 2: Determine final stack state
The console.log prints the current stack array: [10, 30].
Final Answer:
[10, 30] -> Option B
Quick Check:
Push and pop update stack correctly [OK]
Hint: Pop removes last pushed element, then push adds new [OK]
Common Mistakes:
Forgetting pop removes last element
Assuming pop removes first element
Confusing order of push and pop
4. Identify the error in the following TypeScript code for stack pop operation and select the correct fix.
class Stack {
private items: number[] = [];
pop(): number {
return this.items.pop();
}
}
medium
A. pop() should check if stack is empty before popping
B. items array should be public, not private
C. pop() should return void, not number
D. No error, code is correct
Solution
Step 1: Analyze pop method behavior
The pop() method of array returns number | undefined if empty. Returning directly without check can cause undefined return.
Step 2: Identify fix for empty stack
To avoid returning undefined, pop() should check if items is empty before popping and handle that case.
Final Answer:
pop() should check if stack is empty before popping -> Option A
Quick Check:
Check empty before pop to avoid undefined [OK]
Hint: Always check stack empty before pop to avoid errors [OK]
Common Mistakes:
Ignoring empty stack check
Changing return type incorrectly
Making items public unnecessarily
5. Given a stack implemented using an array, how would you implement a function reverseStack that reverses the elements in the stack without using any extra stack or array? Which approach is correct?
hard
A. Use recursion to pop all elements, then insert them at bottom recursively
B. Use a for loop to swap elements from start and end indices
C. Pop all elements into a new array, then push back in reverse order
D. Use a queue to dequeue and enqueue elements to reverse order
Solution
Step 1: Understand constraints
No extra stack or array allowed, so iterative swapping or extra storage is not possible.
Step 2: Use recursion to reverse stack
Recursion can pop all elements, then insert each element at the bottom of the stack recursively, effectively reversing it.
Step 3: Evaluate other options
Use a for loop to swap elements from start and end indices requires direct index access which is not typical for stack abstraction. Pop all elements into a new array, then push back in reverse order uses extra array. Use a queue to dequeue and enqueue elements to reverse order uses queue, which is extra data structure.
Final Answer:
Use recursion to pop all elements, then insert them at bottom recursively -> Option A
Quick Check:
Recursion reverses stack without extra storage [OK]
Hint: Recursion can reverse stack by inserting at bottom [OK]
Common Mistakes:
Using extra arrays or stacks against constraints
Trying to swap elements directly in stack
Using other data structures like queue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.