{
  "slug": "queue-enqueue-dequeue",
  "title": "Queue — Enqueue & Dequeue",
  "category": "Stacks & Queues",
  "difficulty": "intro",
  "complexity": {
    "time": "O(1) amortised",
    "space": "O(n)"
  },
  "idea": "A queue adds at one end and removes from the other. The value that has waited longest is always served first — first in, first out, exactly like a line of people.",
  "code": [
    "enqueue(v): add v at the back",
    "dequeue():  remove from the front",
    "front():    look at the front value"
  ],
  "example": {
    "array": [
      4,
      8,
      1,
      6
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 10,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "A queue: values join at the back and leave from the front.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [],
      "focus": [],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "insert",
      "caption": "Enqueue 4 at the back.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4
      ],
      "focus": [
        "insert"
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 0,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "insert",
      "caption": "Enqueue 8 at the back.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4,
        8
      ],
      "focus": [
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 1,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "insert",
      "caption": "Enqueue 1 at the back.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4,
        8,
        1
      ],
      "focus": [
        null,
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 2,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "insert",
      "caption": "Enqueue 6 at the back.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4,
        8,
        1,
        6
      ],
      "focus": [
        null,
        null,
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 3,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "remove",
      "caption": "Dequeue — 4 arrived first, so it leaves first.",
      "note": "4 leaves",
      "codeLine": 2,
      "stats": null,
      "array": [
        4,
        8,
        1,
        6
      ],
      "focus": [
        "remove",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 3,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "remove-close",
      "caption": "Everyone behind shuffles forward one place.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        8,
        1,
        6
      ],
      "focus": [
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 3,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "remove",
      "caption": "Dequeue — 8 arrived first, so it leaves first.",
      "note": "8 leaves",
      "codeLine": 2,
      "stats": null,
      "array": [
        8,
        1,
        6
      ],
      "focus": [
        "remove",
        null,
        null
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 2,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "remove-close",
      "caption": "Everyone behind shuffles forward one place.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        6
      ],
      "focus": [
        null,
        null
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 2,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 9,
      "op": "say",
      "caption": "2 values still waiting.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        6
      ],
      "focus": [
        null,
        null
      ],
      "markers": [
        {
          "name": "front",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "rear",
          "index": 1,
          "tone": "pivot"
        }
      ],
      "regions": []
    }
  ]
}