3.2.8 · HinglishLinear Data Structures

Deque (double-ended queue) — operations, use cases

1,689 words8 min readRead in English

3.2.8 · Coding › Linear Data Structures


Deque HAI KYA?

Chaar core operations:

Operation Matlab
push_front(x) x ko front pe add karo
push_back(x) x ko rear pe add karo
pop_front() front element ko remove karke return karo
pop_back() rear element ko remove karke return karo
front() / back() bina remove kiye peek karo
Figure — Deque (double-ended queue) — operations, use cases

KAISE banta hai? (Scratch se Derivation)

Tum ko kabhi "yaad" nahi kar sakte — tumhe dekhna hoga ki har implementation usse kyun deta hai.

Implementation 1 — Doubly Linked List

HEAD <-> [a] <-> [b] <-> [c] <-> TAIL
  • push_front: node banao, HEAD sentinel ke baad splice karo → .
  • pop_back: TAIL.prev directly last node deta hai → .

Yeh kyun kaam karta hai: har end-operation sirf 2–3 pointers ko touch karta hai, size se independent.

Implementation 2 — Circular Dynamic Array (woh fast wala)

front index, size, capacity C rakho. Indices modular arithmetic se wrap hote hain:

Jab size == C, capacity double karo aur copy karo — amortized (wahi logic jaise dynamic array: pushes ka total cost ).


KAB use karte hain? (Use cases — 80/20)

Doosre 20%-jo-80%-cover-karte-hain uses:

  • Ek hi structure mein stack aur queue dono ka behavior (undo + redo).
  • Palindrome check: pop_front() vs pop_back() compare karo jab tak cross na ho jaayein.
  • 0–1 BFS: weight 0 wale edges → push_front, weight 1 wale → push_back (deque priority queue ko replace karta hai).
  • Steal-work scheduling (parallel runtimes mein work-stealing deques).

Worked Examples


Complexity Summary


Recall Feynman: ek 12-saal ke bacche ko explain karo

Ek line of kids imagine karo jo haath pakde khade hain. Normally ek naya bachcha end mein aata hai aur front wala bachcha pehle jaata hai — yeh queue hai. Deque ek magic line hai jahan ek bachcha front YA back mein aa sakta hai, aur front YA back se ja sakta hai, instantly. Beech mein kisi ko bhi shuffle nahi karna padta. Bas itna hi hai — ek line jise tum dono taraf se poke kar sakte ho.


Active Recall

"Deque" ka full form kya hai aur ise kaise pronounce karte hain?
Double-Ended Queue; pronounced "deck".
Chaar core deque operations kaunse hain?
push_front, push_back, pop_front, pop_back (har ek O(1)).
Deque stack aur queue dono ko generalize kaise karta hai?
push_back+pop_back tak restrict karo → stack (LIFO); push_back+pop_front → queue (FIFO).
O(1) deque ke liye doubly linked list kyun chahiye?
pop_back ko last node se pehle wala node instantly chahiye; prev pointer bina head se chale deta hai.
Capacity C ke circular array mein push_front ke liye front ko left move karne ka formula kya hai?
front = (front - 1 + C) mod C; +C negative indices ko rokta hai.
Circular deque mein front se k-wein element ka index?
(front + k) mod C.
Plain array + shifting se deque implement kyun nahi karna chahiye?
push_front shifting ki wajah se O(n) ban jaata hai; O(1) goal ko defeat karta hai. Circular buffer use karo iske bajaye.
Deque ka killer use case aur uski complexity?
Sliding window maximum O(n) mein, useful indices ko decreasing order mein rakho.
Deque 0-1 BFS kaise solve karta hai?
weight-0 edges push_front, weight-1 edges push_back; deque priority queue ko replace karta hai.
Dynamic-array deque mein push ka amortized cost kya hai aur kyun?
O(1); kabhi kabhi doubling+copy O(n) cost karta hai lekin n pushes mein spread hoke average O(1) deta hai.

Connections

Concept Map

supports

restrict to back only

back push front pop

implemented by

implemented by

needs

gives O 1 pop_back

uses

wraps front rear

when full

yields

Deque double-ended queue

push/pop front and back O 1

Stack LIFO

Queue FIFO

Doubly Linked List

prev pointer

Circular Dynamic Array

Modular index arithmetic

Double capacity and copy

Amortized O 1