Data Structures · Visualization
Deque: Pop Back
Deque
Popping from the back removes and returns the current last item, shrinking the deque from that end — the same as popping a stack. Time: O(1). Space: O(1) per operation.
Big-O: Push/pop at both ends in O(1).
Deque: Pop Back
DequePopping from the back removes and returns the current last item, shrinking the deque from that end — the same as popping a stack. The visualizer's default deque is empty, so popBack() on a fresh page reports that there is nothing to pop; after pushBack(15) the back is 15 and popBack() removes it, leaving []. Because only the back index moves, the cost is O(1) and no remaining element is shifted, which is what makes a deque usable as the engine of a sliding-window maximum.
How Pop Back Works
- Check that the deque is not empty — an empty deque must report that there is nothing to pop.
- Read the value at the back, which is 15 after a pushBack(15) on the default empty deque.
- Step the back index back by one, or unlink the tail node.
- Return the removed value; if the deque is now empty, both references reset.
Pop Back Worked Example
Input: popBack() on the default deque
- The default deque is empty, so popBack() reports that there is nothing to pop.
- After pushBack(15) the deque is [15], and popBack() removes 15 from the back.
- The deque is empty again, exactly as after popFront().
Output: Removed: 15, remaining deque: []
Press ▶ Run to watch it step by step.
from collections import deque
d = deque()
d.append(1) # push back
d.appendleft(2) # push front
d.pop() # pop back
d.popleft() # pop front
# O(1) at both ends
# example: pop back on [1, 2, 3]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Pop Back Time & Space Complexity
When to Use Pop Back
- Undoing the most recently added item
- Removing the largest/smallest tracked element from a monotonic deque
- Popping the most recent step in a history stack
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.