The push operation adds a new element to the top of the stack, increasing its size by one.
Click to reveal answer
beginner
In a stack, what happens if you try to push an element when the stack is full?
This situation is called stack overflow. The push operation cannot add a new element because there is no space left.
Click to reveal answer
beginner
Show the state of the stack after pushing elements 5, then 10, then 15 (starting from empty).
After pushing 5: 5 -> null After pushing 10: 10 -> 5 -> null After pushing 15: 15 -> 10 -> 5 -> null
Click to reveal answer
beginner
What is the time complexity of the push operation on a stack?
The push operation runs in O(1) time because it only adds an element to the top without traversing the stack.
Click to reveal answer
intermediate
Explain the difference between push operation in an array-based stack and a linked-list-based stack.
In an array-based stack, push adds the element at the next free index if space is available. In a linked-list-based stack, push creates a new node and links it as the new top.
Click to reveal answer
What does the push operation do in a stack?
AAdds an element to the top
BRemoves the top element
CChecks if the stack is empty
DReturns the bottom element
✗ Incorrect
Push adds a new element to the top of the stack.
What is stack overflow?
ATrying to pop when stack is empty
BTrying to push when stack is full
CAdding an element to the bottom
DRemoving all elements at once
✗ Incorrect
Stack overflow happens when pushing to a full stack.
What is the time complexity of push operation?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Push operation takes constant time O(1).
In a linked-list stack, what does push do?
ACreates a new node and links it as top
BTraverses to the bottom
CDeletes the last node
DCopies all nodes
✗ Incorrect
Push creates a new node and makes it the new top.
If a stack currently has elements 3 -> 2 -> 1 -> null, what will be the stack after push(4)?
A3 -> 2 -> null
B3 -> 2 -> 1 -> 4 -> null
C4 -> 3 -> 2 -> 1 -> null
D1 -> 2 -> 3 -> 4 -> null
✗ Incorrect
Push adds 4 on top, so 4 becomes the new top.
Describe step-by-step what happens internally when you push an element onto a stack implemented using a linked list.
Think about how the new node connects to the existing stack.
You got /3 concepts.
Explain what stack overflow means and how it affects the push operation in an array-based stack.
Consider the fixed size of the array.
You got /3 concepts.
Practice
(1/5)
1. What does the push operation do in a stack?
easy
A. Checks if the stack is empty
B. Removes the top element from the stack
C. Returns the bottom element of the stack
D. Adds an element to the top of the stack
Solution
Step 1: Understand stack behavior
A stack follows Last In First Out (LIFO) principle, where elements are added or removed from the top.
Step 2: Define push operation
The push operation adds a new element on top of the existing stack elements.
Final Answer:
Adds an element to the top of the stack -> Option D
Quick Check:
Push = Add top element [OK]
Hint: Push means add element on top of stack [OK]
Common Mistakes:
Confusing push with pop operation
Thinking push removes elements
Assuming push checks stack emptiness
2. Which of the following is the correct syntax to push an integer 10 onto a stack named stk in C++ STL?
easy
A. stk.push(10);
B. stk.add(10);
C. stk.insert(10);
D. stk.append(10);
Solution
Step 1: Recall C++ STL stack methods
The standard method to add an element to a stack is push().
Step 2: Match correct syntax
Using stk.push(10); correctly pushes 10 onto the stack.
Final Answer:
stk.push(10); -> Option A
Quick Check:
push() is the correct method [OK]
Hint: Use push() to add elements in C++ stack [OK]
Common Mistakes:
Using add(), insert(), or append() which are not stack methods
Forgetting parentheses after push
Confusing vector methods with stack
3. What will be the output after executing the following C++ code?
std::stack<int> stk;
stk.push(5);
stk.push(10);
stk.push(15);
int topElement = stk.top();
What is the value of topElement?
medium
A. 15
B. 10
C. 0
D. 5
Solution
Step 1: Trace push operations
Elements pushed in order: 5, then 10, then 15. Stack top is last pushed element.
Step 2: Identify top element
The top element after pushes is 15, as it was pushed last.
Final Answer:
15 -> Option A
Quick Check:
Last pushed = top element = 15 [OK]
Hint: Top element is last pushed value [OK]
Common Mistakes:
Choosing first pushed element as top
Confusing top() with pop()
Assuming stack is empty initially
4. Identify the error in the following code snippet that tries to push an element onto a stack:
std::stack<int> stk;
stk.push 20;
medium
A. Using wrong data type for stack
B. Stack not declared properly
C. Missing parentheses in push call
D. No error in the code
Solution
Step 1: Check push syntax
The push method requires parentheses around the argument, like push(20).
Step 2: Identify syntax error
Code uses stk.push 20; missing parentheses, causing a syntax error.
Final Answer:
Missing parentheses in push call -> Option C
Quick Check:
push() needs parentheses [OK]
Hint: Always use parentheses with push() [OK]
Common Mistakes:
Omitting parentheses after push
Confusing push with pop syntax
Assuming stack declaration is wrong
5. Given an empty stack, what will be the state of the stack after executing the following code?