{"record":{"id":"7a25d669c4ac26d1","repo":"krahets/hello-algo","slug":"index-out-of-bounds-exception-7a25d6","errorCode":null,"errorMessage":"Index Out Of Bounds Exception","messagePattern":"Index Out Of Bounds Exception","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_graph/graph_adjacency_matrix.ts","lineNumber":52,"sourceCode":"        const n: number = this.size();\n        // Add the value of the new vertex to the vertex list\n        this.vertices.push(val);\n        // Add a row to the adjacency matrix\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // Add a column to the adjacency matrix\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* Remove vertex */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // Remove the vertex at index from the vertex list\n        this.vertices.splice(index, 1);\n\n        // Remove the row at index from the adjacency matrix\n        this.adjMat.splice(index, 1);\n        // Remove the column at index from the adjacency matrix\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* Add edge */\n    // Parameters i, j correspond to the vertices element indices\n    addEdge(i: number, j: number): void {\n        // Handle index out of bounds and equality\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_graph/graph_adjacency_matrix.ts#L34-L70","documentation":"Thrown by GraphAdjacencyMatrix.removeVertex when the supplied index is out of range. The matrix is index-addressed, so removal splices both the vertex list and the matrix rows/columns by that index; an out-of-range index would corrupt the structure. Note: the guard only checks index >= size(), not index < 0, so negative indices are a latent separate bug.","triggerScenarios":"Passing an index >= current vertex count; passing an index that was valid before prior removeVertex calls shrank the list; passing a negative index (will not throw here, but misbehaves via splice).","commonSituations":"Iterating a shrinking vertex list with stale upper bounds; off-by-one when translating from 1-based external ids; using array length instead of graph.size() after mutations.","solutions":["Bounds-check with 0 <= index < graph.size() before calling removeVertex.","Recompute size() immediately before removal in loops that mutate the graph.","Iterate from high to low index when removing multiple vertices so earlier indices stay valid.","Add an index < 0 guard in your caller since the library only checks the upper bound."],"exampleFix":"// before\nfor (let i = 0; i < n; i++) graph.removeVertex(i); // throws after size shrinks\n\n// after\nfor (let i = n - 1; i >= 0; i--) graph.removeVertex(i);","handlingStrategy":"validation","validationCode":"function safeRemoveVertex(g, i) {\n  if (i >= 0 && i < g.size()) g.removeVertex(i);\n}","typeGuard":"function isValidIndex(g, i) {\n  return Number.isInteger(i) && i >= 0 && i < g.size();\n}","tryCatchPattern":"try { graph.removeVertex(i); }\ncatch (e) { if (!(e instanceof RangeError)) throw e; /* skip */ }","preventionTips":["Always check both bounds; the library only checks the upper bound.","Remove from high index to low to keep earlier indices valid.","Recompute size() in any loop that mutates the graph."],"tags":["graph","typescript","validation","guard-clause","off-by-one","bounds"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}