{
  "slug": "stack-push-pop",
  "title": "Stack — Push & Pop",
  "category": "Stacks & Queues",
  "difficulty": "intro",
  "complexity": {
    "time": "O(1) per op",
    "space": "O(n)"
  },
  "idea": "A stack only ever touches its top. Push puts a value on top; pop takes the top one off. The most recently added value is always the first to leave — last in, first out.",
  "code": [
    "push(v): put v on the top",
    "pop():   take the top value off",
    "peek():  look at the top without removing"
  ],
  "example": {
    "array": [
      3,
      7,
      2,
      9
    ]
  },
  "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 stack grows upward. Values go on the top and come off the top.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [],
      "focus": [],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "insert",
      "caption": "Push 3 — it lands on top.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3
      ],
      "focus": [
        "insert"
      ],
      "markers": [
        {
          "name": "top",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "insert",
      "caption": "Push 7 — it lands on top.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        7
      ],
      "focus": [
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "top",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "insert",
      "caption": "Push 2 — it lands on top.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        7,
        2
      ],
      "focus": [
        null,
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "top",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "insert",
      "caption": "Push 9 — it lands on top.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        7,
        2,
        9
      ],
      "focus": [
        null,
        null,
        null,
        "insert"
      ],
      "markers": [
        {
          "name": "top",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "remove",
      "caption": "Pop — 9 went on last, so it comes off first.",
      "note": "9 leaves",
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        7,
        2,
        9
      ],
      "focus": [
        null,
        null,
        null,
        "remove"
      ],
      "markers": [
        {
          "name": "top",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "remove-close",
      "caption": "9 is gone; the value below is the new top.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        7,
        2
      ],
      "focus": [
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "top",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "remove",
      "caption": "Pop — 2 went on last, so it comes off first.",
      "note": "2 leaves",
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        7,
        2
      ],
      "focus": [
        null,
        null,
        "remove"
      ],
      "markers": [
        {
          "name": "top",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "remove-close",
      "caption": "2 is gone; the value below is the new top.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        7
      ],
      "focus": [
        null,
        null
      ],
      "markers": [
        {
          "name": "top",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 9,
      "op": "say",
      "caption": "2 values left on the stack.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        7
      ],
      "focus": [
        null,
        null
      ],
      "markers": [
        {
          "name": "top",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": []
    }
  ]
}