Data Structures · Visualization

Deque: Push Front

Deque

Pushing to the front adds a new item before the current first item, making it the new front. Time: O(1). Space: O(1) per operation.

Big-O: Push/pop at both ends in O(1).

Deque: Push Front

Deque

Pushing to the front adds a new item before the current first item, making it the new front. The visualizer starts from an empty deque and pushes its default value 15, so the first frame holds [15]; pushing 15 again would give [15, 15] because each call inserts ahead of whatever is already there. A deque is built so this is exactly as cheap as pushing at the back: a doubly linked list rewires two pointers, and a circular array decrements the front index modulo capacity. That keeps push-front O(1) while capacity remains, with a reallocation as the only occasional O(n) cost.

How Push Front Works

  1. Take the new value to add — the visualizer uses 15.
  2. Check capacity: a bounded circular deque must not overwrite the back when the ring is full.
  3. Place it before the current front item, or at index front - 1 modulo capacity.
  4. Update the 'front' reference to point to this new item.
  5. Leave the back reference alone unless the deque was empty, in which case both ends name the new item.

Push Front Worked Example

Input: pushFront(15) on the default empty deque

  1. The visualizer's default deque is empty, so there is no front to move aside.
  2. 15 becomes the only item: [15], and it is both the front and the back.
  3. A second pushFront(15) would insert ahead of it and produce [15, 15].

Output: [15]

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: push front 15 on [1, 2, 3]
Input: pushFront(15) on the default empty 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.

Push Front Time & Space Complexity

TimeO(1)
SpaceO(1) per operation

When to Use Push Front

  • Adding a high-priority task to the very front of a work queue
  • Implementing 'undo' by pushing the previous state to the front
  • Sliding window algorithms that remove/add at the front as the window moves

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.