{
  "slug": "array-traverse",
  "title": "Traverse an Array",
  "category": "Arrays",
  "difficulty": "intro",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Visit each index in order, doing constant work per element — the foundation of almost every array algorithm.",
  "code": [
    "for i in 0 .. n-1:",
    "    visit a[i]",
    "done"
  ],
  "example": {
    "array": [
      4,
      8,
      15,
      16,
      23,
      42
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 8,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Walk the array left to right — 6 elements.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "read",
      "caption": "Index 0 holds 4.",
      "note": "running sum = 4",
      "codeLine": 2,
      "stats": {
        "sum": 4
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        "read",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 0,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "read",
      "caption": "Index 1 holds 8.",
      "note": "running sum = 12",
      "codeLine": 2,
      "stats": {
        "sum": 12
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "read",
      "caption": "Index 2 holds 15.",
      "note": "running sum = 27",
      "codeLine": 2,
      "stats": {
        "sum": 27
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "read",
      "caption": "Index 3 holds 16.",
      "note": "running sum = 43",
      "codeLine": 2,
      "stats": {
        "sum": 43
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        null,
        "read",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "read",
      "caption": "Index 4 holds 23.",
      "note": "running sum = 66",
      "codeLine": 2,
      "stats": {
        "sum": 66
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "read",
      "caption": "Index 5 holds 42.",
      "note": "running sum = 108",
      "codeLine": 2,
      "stats": {
        "sum": 108
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "read"
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "say",
      "caption": "Done — every element visited. The sum is 108.",
      "note": null,
      "codeLine": 3,
      "stats": {
        "sum": 108
      },
      "array": [
        4,
        8,
        15,
        16,
        23,
        42
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    }
  ]
}