Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Queues

First in, first out - handle things in the order they arrived.

Beginner6 problems2 interactive subtopicsO(1) per operation
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/4ping(1)
window starts at −2999
0
1
front
back
  • active this step
  • removed / popped
Space Play←→ StepR Replay
1 / 4
PYTHONping(1), ping(100), ping(3001), ping(3002)
1from collections import deque
2
3class RecentCounter:
4 def __init__(self):
5 self.q = deque()
6
7 def ping(self, t):
8 self.q.append(t)
9 while self.q[0] < t - 3000:
10 self.q.popleft()
▶11 return len(self.q)
STEP 1 OF 4Setup

ping(1)

The queue is [1]. Nothing is older than 1 − 3000, so it returns 1.

VARIABLES

t
1
q
[1]
returns
1
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A queue adds at the back and removes from the front. In Python, collections.deque gives O(1) operations at both ends.

Why it matters

Scheduling, buffering and breadth-first search all depend on handling the oldest item first.

Think of it like this

A line at a ticket counter: whoever arrived first is served first.

Use it when

  • Processing items in arrival order
  • Level-by-level traversal (BFS)
  • Time windows over a stream of events

Avoid it when

  • You need the most recent item first - use a stack
  • You need the smallest item first - use a heap

Complexity cheatsheet

OperationTimeSpace
Enqueue (append)O(1)O(1)
Dequeue (popleft)O(1)O(1)
list.pop(0) insteadO(n)O(1)

Often confused with: priority queues - they serve by priority, not by arrival time.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

in orderlevel by levelfirst come, first servedrecent requestsstream
  • list.pop(0) is O(n); deque.popleft() is O(1)
  • Time-window counters often keep a queue of timestamps
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Using list.pop(0) - every dequeue shifts the whole list
  • Reading q[0] on an empty queue
  • Mixing up which end you add to and remove from

Edge cases to test

  • An empty queue before the first operation
  • Many items expiring at once
  • Equal timestamps
Interview tip: Name collections.deque - it shows you know the O(n) trap with lists.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Which Python structure removes from the front in O(1)?

06

Practice

Apply what you just watched on real interview problems.

Generate Stack problem in AI DSA Practice →
  • BeginnerNumber of Recent CallsLeetCode 933 ↗ (opens in a new tab)
  • IntermediateReveal Cards In Increasing OrderLeetCode 950 ↗ (opens in a new tab)
  • AdvancedDesign Circular DequeLeetCode 641 ↗ (opens in a new tab)
StacksHash Maps