The 'peek' operation returns the top element of the stack without removing it, allowing you to see the current top value.
Click to reveal answer
beginner
In a stack, what happens if you try to peek when the stack is empty?
Peeking an empty stack usually results in an error or a special value indicating the stack is empty, because there is no top element to return.
Click to reveal answer
beginner
Why is peek useful compared to pop in a stack?
Peek lets you see the top element without removing it, so you can check the value without changing the stack's contents, unlike pop which removes the top element.
Click to reveal answer
beginner
Show the C++ code snippet to peek the top element of a stack named 's'.
if (!s.empty()) {
int topElement = s.top();
// use topElement as needed
} else {
// handle empty stack case
}
Click to reveal answer
beginner
What is the time complexity of the peek operation on a stack?
The peek operation runs in O(1) time because it directly accesses the top element without any iteration.
Click to reveal answer
What does the peek operation on a stack return?
AThe top element without removing it
BThe bottom element of the stack
CRemoves and returns the top element
DRemoves the bottom element
✗ Incorrect
Peek returns the top element without removing it, unlike pop which removes the top element.
What happens if you peek an empty stack?
AReturns the bottom element
BReturns an error or special value
CReturns zero always
DRemoves the top element
✗ Incorrect
Peeking an empty stack usually causes an error or returns a special value indicating no elements.
Which C++ stack method is used to peek the top element?
Apop()
Bpeek()
Ctop()
Dfront()
✗ Incorrect
The top() method returns the top element without removing it.
What is the time complexity of the peek operation on a stack?
AO(n^2)
BO(log n)
CO(n)
DO(1)
✗ Incorrect
Peek runs in constant time O(1) because it accesses the top element directly.
Why might you use peek instead of pop?
ATo check the top element without removing it
BTo remove the top element
CTo clear the stack
DTo add a new element
✗ Incorrect
Peek lets you see the top element without changing the stack.
Explain how the peek operation works on a stack and why it is useful.
Think about how peek differs from pop.
You got /4 concepts.
Describe how to safely peek the top element of a stack in C++.
Consider what happens if the stack has no elements.
You got /4 concepts.
Practice
(1/5)
1. What does the peek() operation do in a stack?
easy
A. Returns the top element without removing it
B. Removes the top element from the stack
C. Adds a new element to the top of the stack
D. Checks if the stack is empty
Solution
Step 1: Understand stack operations
A stack is a collection where elements are added or removed from the top only.
Step 2: Define peek operation
The peek operation returns the top element without removing it, unlike pop which removes it.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
peek() = top element [OK]
Hint: Peek shows top without removal [OK]
Common Mistakes:
Confusing peek with pop
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct syntax to peek the top element of a stack named stk in C++ STL?
easy
A. stk.pop();
B. stk.peek();
C. stk.top();
D. stk.getTop();
Solution
Step 1: Recall C++ STL stack methods
The standard method to access the top element is top().
Step 2: Identify correct syntax
stk.top(); returns the top element without removing it.
Final Answer:
stk.top(); -> Option C
Quick Check:
stk.top() = peek top element [OK]
Hint: Use .top() to peek in C++ STL stack [OK]
Common Mistakes:
Using .peek() which is not in C++ STL
Using .pop() which removes element
Using non-existent .getTop() method
3. What will be the output of the following C++ code?
Elements pushed in order: 10, then 20, then 30. Stack top is last pushed element 30.
Step 2: Check top() output
stk.top() returns 30 without removing it.
Final Answer:
30 -> Option A
Quick Check:
Last pushed = top = 30 [OK]
Hint: Top is last pushed element [OK]
Common Mistakes:
Confusing top with bottom element
Assuming top removes element
Thinking output is 20 or 10
4. Identify the error in the following code snippet that tries to peek the top element of a stack named stk:
int topElement = stk.peek();
medium
A. stk should be declared as a queue
B. peek() is not a valid method in C++ STL stack
C. topElement should be a pointer
D. stk.pop() should be used instead
Solution
Step 1: Check method availability in C++ STL stack
C++ STL stack does not have a method named peek().
Step 2: Correct method to peek top element
The correct method is top(), so stk.top() should be used.
Final Answer:
peek() is not a valid method in C++ STL stack -> Option B
Quick Check:
Use stk.top() not stk.peek() [OK]
Hint: Use .top(), not .peek() in C++ STL [OK]
Common Mistakes:
Using peek() method which doesn't exist
Confusing pop() with peek()
Thinking topElement must be pointer
5. Given a stack stk with elements [5, 10, 15] pushed in that order, which code snippet correctly prints the top element without removing it and then prints the stack size?