krahets/hello-algo · error · RangeError
Index Out Of Bounds Exception
Error message
Index Out Of Bounds Exception
What it means
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.
Source
Thrown at en/codes/typescript/chapter_graph/graph_adjacency_matrix.ts:52
const n: number = this.size();
// Add the value of the new vertex to the vertex list
this.vertices.push(val);
// Add a row to the adjacency matrix
const newRow: number[] = [];
for (let j: number = 0; j < n; j++) {
newRow.push(0);
}
this.adjMat.push(newRow);
// Add a column to the adjacency matrix
for (const row of this.adjMat) {
row.push(0);
}
}
/* Remove vertex */
removeVertex(index: number): void {
if (index >= this.size()) {
throw new RangeError('Index Out Of Bounds Exception');
}
// Remove the vertex at index from the vertex list
this.vertices.splice(index, 1);
// Remove the row at index from the adjacency matrix
this.adjMat.splice(index, 1);
// Remove the column at index from the adjacency matrix
for (const row of this.adjMat) {
row.splice(index, 1);
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
addEdge(i: number, j: number): void {
// Handle index out of bounds and equality
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');View on GitHub (pinned to 69932aed18)
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.
Example fix
// before for (let i = 0; i < n; i++) graph.removeVertex(i); // throws after size shrinks // after for (let i = n - 1; i >= 0; i--) graph.removeVertex(i);
Defensive patterns
Strategy: validation
Validate before calling
function safeRemoveVertex(g, i) {
if (i >= 0 && i < g.size()) g.removeVertex(i);
} Type guard
function isValidIndex(g, i) {
return Number.isInteger(i) && i >= 0 && i < g.size();
} Try / catch
try { graph.removeVertex(i); }
catch (e) { if (!(e instanceof RangeError)) throw e; /* skip */ } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Index Out Of Bounds Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Heap is empty.
- The Deque Is Empty.
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/7a25d669c4ac26d1.
Report an issue: GitHub.