Recall & Review
beginner
What does the 'peek' operation do in a stack?
The 'peek' operation shows the top element of the stack without removing it. It lets you see what's on top without changing the stack.
Click to reveal answer
beginner
Why is 'peek' useful in stack operations?
Peek helps check the top item before deciding to pop or push. It is useful for decision-making without changing the stack.
Click to reveal answer
beginner
What happens if you peek an empty stack?
Peeking an empty stack usually returns None or raises an error because there is no top element to show.
Click to reveal answer
beginner
Code snippet: How to peek the top element in a Python list used as a stack?
stack = [1, 2, 3] top = stack[-1]
Using stack[-1] gets the last item in the list, which is the top of the stack. Here, top will be 3.
Click to reveal answer
beginner
Explain the difference between 'pop' and 'peek' in a stack.
'Pop' removes and returns the top element, changing the stack. 'Peek' only returns the top element without removing it.
Click to reveal answer
What does the peek operation return in a stack?
✗ Incorrect
Peek returns the top element without removing it from the stack.
What happens if you peek an empty stack?
✗ Incorrect
Peeking an empty stack returns None or causes an error because there is no top element.
Which Python list index gives the top element of a stack implemented as a list?
✗ Incorrect
stack[-1] accesses the last element, which is the top of the stack.
What is the difference between peek and pop?
✗ Incorrect
Pop removes and returns the top element; peek only returns it without removing.
Why might you use peek before pop?
✗ Incorrect
Peek lets you see the top element before deciding to remove it with pop.
Describe how the peek operation works on a stack and why it is useful.
Think about checking the top without changing the stack.
You got /3 concepts.
Explain what happens when you peek an empty stack and how to handle it safely.
Consider what the stack looks like when empty.
You got /3 concepts.