Skip to content

    Queue

    DSA · Beginner

    Definition

    A queue is a First-In-First-Out (FIFO) structure: add at the rear, remove from the front.

    Queues power BFS, task scheduling, printers and message systems.

    Diagram

    FRONT → ABCD ← REAR

    Flowchart

    Start emptyenqueue at reardequeue from frontProcess in arrival order

    Example: Queue with deque

    from collections import deque
    q = deque()
    q.append('A'); q.append('B')
    print(q.popleft())

    Output: A

    Complexity

    enqueue/dequeue O(1)

    Key points

    • FIFO order
    • Circular queue reuses space
    • Priority queue orders by priority

    Interview questions

    Why not use list.pop(0) in Python?

    It is O(n); deque.popleft() is O(1).

    Where is a queue used in graphs?

    Breadth-First Search explores level by level using a queue.

    More DSA topics