Intuition The one core idea
A deque is just a line of items you can add to or remove from at both ends , and every operation stays fast because you only ever touch the two edges — never the middle. Everything on this page exists to make that one sentence rock-solid: what "both ends" means, what "fast" (O ( 1 ) ) means, and the arithmetic that lets an array pretend to be a ring.
Before you can read the parent note Deque , you must own every symbol it throws at you. Below, each idea is built from nothing, drawn as a picture, and justified — why does the topic need this?
The most basic object here is a sequence : items sitting in a row, left to right.
Definition Front and back
The front is the leftmost end of the line.
The back (also called rear ) is the rightmost end.
Both words name positions , not items. The item there can change; the position stays "the left edge" / "the right edge".
Intuition Why the topic needs this
A stack only pokes one end. A queue pokes one end to add and the other to remove. A deque pokes both ends for both actions . So "front" and "back" are the two doors of the whole structure — every operation is named after the door it uses (push_front, pop_back, ...).
Everywhere in this topic you see the letter n .
n
n means the number of items currently in the structure . If the line is [3, 5, 8], then n = 3 .
When you push, n grows by 1. When you pop, n shrinks by 1.
Why a single letter? Because we want to talk about cost for any size at once . Saying "if there are n things" covers 3 things, 3 million things, and everything between in one breath.
This is the symbol the whole topic is bragging about ("O ( 1 ) at both ends!"), so we build it carefully from zero.
Intuition The picture: how does work grow as the line grows?
Imagine feeding your structure more and more items and timing one operation each time. Draw the timing on a graph: horizontal axis = n (size), vertical axis = time. The shape of that curve is what Big-O names.
O ( 1 ) — "constant time"
==The work does not grow when n grows.== One item or one billion items, the operation costs the same handful of steps. On the graph it is a flat horizontal line (the blue line above).
O ( n ) — "linear time"
==The work grows in proportion to n .== Double the items, double the work. On the graph it is a straight slanted line rising with n (the orange line above).
O ( nk ) and O ( n 2 )
O ( nk ) : work grows with two sizes multiplied — here n (array length) times k (window width).
O ( n 2 ) (read "n squared", n × n ): work grows with the size times itself — the steep red curve. This is what happens if you shift a whole array on every insert.
Intuition Why the topic needs Big-O
The entire selling point of a deque is "both ends in O ( 1 ) " . Without the flat-line meaning of O ( 1 ) , that promise is just noise. And the villain of the story — the plain-array shift — is bad precisely because it is O ( n ) per push, giving O ( n 2 ) total. You can't judge the design without this vocabulary.
O ( 1 ) means one single step."
Why it feels right: the "1" looks like a count.
Why it's wrong: O ( 1 ) means bounded by a constant — could be 3 steps or 300, as long as it doesn't grow with n . Fix: read O ( 1 ) as "flat line," not "exactly one instruction."
The parent says push is "amortized O ( 1 ) ". That word is a symbol too.
Most pushes are cheap (O ( 1 ) ). Occasionally the array is full, so you allocate a bigger array and copy everything — that one push costs O ( n ) . Amortized asks: spread that rare big cost over all the cheap pushes — what's the average?
Definition Amortized cost
The total cost of a long run of operations, divided by the number of operations. Even though a few pushes are expensive, doing n pushes costs O ( n ) total , so the average per push is O ( n ) / n = O ( 1 ) .
Intuition Why the topic needs it
The circular-array deque doubles capacity when full. If we only quoted worst-case, push would look like O ( n ) and the structure would seem broken. Amortized analysis is the honest lens that shows the typical cost is still flat. The full argument lives in Dynamic Array Amortized Analysis .
The fast implementation is an array, so we nail its three symbols.
Definition Array, index, capacity
An array is a fixed row of numbered slots in memory.
An index is the slot's number. We count from 0 : the first slot is index 0 , then 1 , 2 , ... So an array with C slots has valid indices 0 through C − 1 .
==C (capacity)== is the total number of slots — how many items fit before we must grow.
Common mistake Off-by-one: "
C slots means indices 0 to C."
Why it wrong: counting from 0, the last valid index is C − 1 , not C . Index C is one step past the end. Fix: remember "0-based": C slots → indices 0 … C − 1 .
This is the single most important piece of arithmetic in the array deque, so we earn it slowly.
a mod C
a mod C (read "a mod C") means ==the remainder after dividing a by C ==. Example: 7 mod 5 = 2 , because 7 = 1 × 5 + 2 .
Why does this build a ring ? Watch what mod 5 does to the counting numbers:
0 , 1 , 2 , 3 , 4 , → 0 5 , → 1 6 , → 2 7 , …
The moment you reach the capacity 5 , the result wraps back to 0 . So counting "past the end" of the array silently lands you at the beginning — exactly a circle.
Intuition Why the topic chose
mod and not, say, an if check
We could write if (index == C) index = 0;. But mod handles any overshoot in one clean formula, including jumps and negative steps. It answers the exact question the deque keeps asking: "I walked off the edge — which real slot am I on?" That is why the parent's index formula is index ( k ) = ( f r o n t + k ) mod C .
The other implementation uses nodes and pointers.
Definition Node and pointer
A node is a little box holding one item plus arrows to its neighbours.
A pointer is an arrow that says "the next box lives here in memory." Following it is one instant step.
A doubly linked list gives every node two pointers: next (to the right neighbour) and prev (to the left neighbour).
doubly — why the topic insists on prev
To pop_back, you must reach the node just before the last one, so you can rewire it as the new last. With only next arrows, you'd start at the front and walk all the way — that's O ( n ) . A prev arrow lets you step backwards one hop, giving O ( 1 ) . This is built in detail in Doubly Linked List .
LIFO = "Last In, First Out." The most recent arrival leaves first — like a stack of plates. This is a Stack .
FIFO = "First In, First Out." The earliest arrival leaves first — like a checkout line. This is a Queue .
Intuition Why the topic needs both
A deque is the superset : lock it to one end and you get a stack; add at one end, remove at the other, and you get a queue. Knowing LIFO/FIFO is how you recognise "oh, this deque is just being used as a stack here."
Sequence with front and back
What do "front" and "back" name — items or positions? Positions: the left edge and right edge of the line; the item sitting there can change.
What does the symbol n stand for? The current number of items in the structure.
In one phrase, what does O ( 1 ) look like on a size-vs-time graph? A flat horizontal line — work does not grow with n .
In one phrase, what does O ( n ) look like? A straight slanted line rising in proportion to n .
Does O ( 1 ) mean "exactly one step"? No — it means bounded by a constant that doesn't grow with n (could be 3 or 300 steps).
What does "amortized O ( 1 ) " mean? Total cost over n operations is O ( n ) , so the average per operation is O ( 1 ) , even if a few are expensive.
For an array of capacity C , what are the valid indices? 0 through C − 1 (0-based counting).
Compute 7 mod 5 . 2 (the remainder of 7 ÷ 5 ).
Why add + C before taking mod C when moving front left? f r o n t − 1 can be − 1 ; adding C first keeps the result non-negative before the remainder.
Move front left once from 0 with C = 5 : what index? ( 0 − 1 + 5 ) mod 5 = 4 .
Why must the linked-list deque be doubly linked? pop_back needs the node before the last instantly; the prev pointer gives it in O ( 1 ) instead of walking O ( n ) .
What do LIFO and FIFO stand for? Last-In-First-Out (stack) and First-In-First-Out (queue).
Parent: Deque — the topic these foundations serve
Stack — the LIFO restriction of a deque
Queue — the FIFO restriction of a deque
Circular Buffer / Ring Buffer — where the mod C ring lives
Doubly Linked List — where prev/next pointers are built
Dynamic Array Amortized Analysis — the full amortized-O ( 1 ) argument