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

Deque

Popping 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

  1. Check that the deque is not empty — an empty deque must report that there is nothing to pop.
  2. Read the value at the back, which is 15 after a pushBack(15) on the default empty deque.
  3. Step the back index back by one, or unlink the tail node.
  4. Return the removed value; if the deque is now empty, both references reset.

Pop Back Worked Example

Input: popBack() on the default deque

  1. The default deque is empty, so popBack() reports that there is nothing to pop.
  2. After pushBack(15) the deque is [15], and popBack() removes 15 from the back.
  3. The deque is empty again, exactly as after popFront().

Output: Removed: 15, remaining deque: []

Try your own input
Deque: Introduction
A "double-ended queue" — values can be added or removed from both the front and the back.
Empty deque
Active / window Result
Deque: Introduction
Press ▶ Run to watch it step by step.
Python · Deque in Code
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]
Input: popBack() on the default deque

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

TimeO(1)
SpaceO(1) per operation

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.