{
  "id": "stacks-queues/queue",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/stacks-queues/queue",
  "topic": {
    "slug": "stacks-queues",
    "title": "Stacks & Queues"
  },
  "title": "Queue",
  "tagline": "First in, first out. Fairness, as a data structure.",
  "vizKind": "queue",
  "complexity": {
    "time": {
      "best": "O(1) enqueue",
      "average": "O(1) dequeue",
      "worst": "O(n) if built badly"
    },
    "space": "O(n)",
    "growth": "constant",
    "note": "Both operations should be O(1). But if you build a queue on a plain array and dequeue with shift(), every removal shuffles the whole array forward — quietly turning an O(1) operation into O(n). Use a proper deque."
  },
  "code": {
    "javascript": "class Queue {\n  constructor() { this.items = []; }\n  enqueue(value) {\n    this.items.push(value);\n  }\n  dequeue() {\n    if (this.isEmpty()) return null;\n    return this.items.shift();\n  }\n  front() {\n    return this.items[0];\n  }\n  isEmpty() {\n    return this.items.length === 0;\n  }\n}",
    "python": "from collections import deque\n\nclass Queue:\n    def __init__(self): self.items = deque()\n\n    def enqueue(self, value):\n        self.items.append(value)\n\n    def dequeue(self):\n        if self.is_empty(): return None\n        return self.items.popleft()\n\n    def front(self):\n        return self.items[0]\n\n    def is_empty(self):\n        return len(self.items) == 0\n",
    "java": "class Queue {\n  private Deque<Integer> items = new ArrayDeque<>();\n\n  void enqueue(int value) {\n    items.addLast(value);\n  }\n  Integer dequeue() {\n    if (isEmpty()) return null;\n    return items.pollFirst();\n  }\n  Integer front() {\n    return items.peekFirst();\n  }\n  boolean isEmpty() {\n    return items.isEmpty();\n  }\n}",
    "cpp": "class Queue {\n  deque<int> items;\npublic:\n  void enqueue(int value) {\n    items.push_back(value);\n  }\n  int dequeue() {\n    if (isEmpty()) return -1;\n    int f = items.front();\n    items.pop_front();\n    return f;\n  }\n  int front() { return items.front(); }\n  bool isEmpty() {\n    return items.empty();\n  }\n};"
  },
  "defaultInput": [
    3,
    7,
    1,
    9
  ],
  "defaultTarget": null,
  "inputHint": "These join the line one by one. Then two get served.",
  "pitfalls": [
    {
      "wrong": "Building a queue on an array and using shift() to dequeue.",
      "right": "shift() moves every remaining element left. On a big queue that's O(n) per removal. Use a linked list or a deque, where the front is just a pointer."
    },
    {
      "wrong": "Mixing up which end is which.",
      "right": "Add at the back, remove from the front. Do both at the same end by accident and you've built a stack."
    },
    {
      "wrong": "Dequeuing from an empty queue.",
      "right": "Same trap as an empty stack. Check first — an empty line has nobody to serve."
    }
  ],
  "quiz": [
    {
      "q": "You enqueue 1, 2, 3, then dequeue once. What comes out?",
      "options": [
        "1",
        "2",
        "3",
        "Nothing"
      ],
      "answer": 0,
      "why": "1 arrived first, so 1 leaves first. First in, first out — the opposite of a stack."
    },
    {
      "q": "What's the real difference between a stack and a queue?",
      "options": [
        "A queue is faster",
        "A stack is sorted",
        "Which end you remove from",
        "A queue can hold more items"
      ],
      "answer": 2,
      "why": "That's genuinely all it is. Both add to one end; a stack removes from the same end, a queue removes from the other. One decision, two completely different tools."
    },
    {
      "q": "Why can dequeuing from an array-based queue be slow?",
      "options": [
        "Arrays can't hold enough items",
        "Removing the front means shuffling everything left",
        "Arrays are always slow",
        "It isn't — it's always fast"
      ],
      "answer": 1,
      "why": "An array has no hole in the middle, so removing the front makes every other element shuffle down one. That's O(n) per dequeue — the exact cost you saw in the Arrays lesson."
    }
  ],
  "problems": [
    {
      "name": "Implement Queue using Stacks",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/implement-queue-using-stacks/"
    },
    {
      "name": "Number of Recent Calls",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/number-of-recent-calls/"
    },
    {
      "name": "Design Circular Queue",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/design-circular-queue/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 10,
    "frames": [
      {
        "data": {
          "items": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "size": 0
        },
        "explanation": "A queue is the line at a ticket counter. You join at the back, and you get served from the front. No cutting.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q0-3",
              "value": 3
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 4,
        "variables": {
          "enqueued": 3,
          "size": 1
        },
        "explanation": "enqueue(3) — 3 joins the back of the line.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q0-3",
              "value": 3
            },
            {
              "id": "q1-7",
              "value": 7
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 4,
        "variables": {
          "enqueued": 7,
          "size": 2
        },
        "explanation": "enqueue(7) — 7 joins the back of the line.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q0-3",
              "value": 3
            },
            {
              "id": "q1-7",
              "value": 7
            },
            {
              "id": "q2-1",
              "value": 1
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          }
        ],
        "codeLine": 4,
        "variables": {
          "enqueued": 1,
          "size": 3
        },
        "explanation": "enqueue(1) — 1 joins the back of the line.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q0-3",
              "value": 3
            },
            {
              "id": "q1-7",
              "value": 7
            },
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 4,
        "variables": {
          "enqueued": 9,
          "size": 4
        },
        "explanation": "enqueue(9) — 9 joins the back of the line.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q0-3",
              "value": 3
            },
            {
              "id": "q1-7",
              "value": 7
            },
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 7,
        "variables": {
          "serving": 3,
          "size": 4
        },
        "explanation": "dequeue() — 3 has been waiting longest, so 3 is served first.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q1-7",
              "value": 7
            },
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          }
        ],
        "codeLine": 8,
        "variables": {
          "dequeued": 3,
          "size": 3
        },
        "explanation": "3 leaves. Everyone shuffles forward and 7 is now at the front.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q1-7",
              "value": 7
            },
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 7,
        "variables": {
          "serving": 7,
          "size": 3
        },
        "explanation": "dequeue() — 7 has been waiting longest, so 7 is served first.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [
          {
            "name": "front",
            "index": 0
          },
          {
            "name": "back",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          }
        ],
        "codeLine": 8,
        "variables": {
          "dequeued": 7,
          "size": 2
        },
        "explanation": "7 leaves. Everyone shuffles forward and 1 is now at the front.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "items": [
            {
              "id": "q2-1",
              "value": 1
            },
            {
              "id": "q3-9",
              "value": 9
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 14,
        "variables": {
          "size": 2,
          "isEmpty": false
        },
        "explanation": "2 still waiting. First in, first out — the exact opposite of a stack, and the reason queues are what you use for anything that must stay fair.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      }
    ]
  }
}