{"record":{"id":"f39dfa67de78cd12","repo":"trekhleb/javascript-algorithms","slug":"position-is-out-of-allowed-range","errorCode":null,"errorMessage":"Position is out of allowed range","messagePattern":"Position is out of allowed range","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/data-structures/tree/fenwick-tree/FenwickTree.js","lineNumber":24,"sourceCode":"   * @param  {number} arraySize\n   */\n  constructor(arraySize) {\n    this.arraySize = arraySize;\n\n    // Fill tree array with zeros.\n    this.treeArray = Array(this.arraySize + 1).fill(0);\n  }\n\n  /**\n   * Adds value to existing value at position.\n   *\n   * @param  {number} position\n   * @param  {number} value\n   * @return {FenwickTree}\n   */\n  increase(position, value) {\n    if (position < 1 || position > this.arraySize) {\n      throw new Error('Position is out of allowed range');\n    }\n\n    for (let i = position; i <= this.arraySize; i += (i & -i)) {\n      this.treeArray[i] += value;\n    }\n\n    return this;\n  }\n\n  /**\n   * Query sum from index 1 to position.\n   *\n   * @param  {number} position\n   * @return {number}\n   */\n  query(position) {\n    if (position < 1 || position > this.arraySize) {\n      throw new Error('Position is out of allowed range');","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/data-structures/tree/fenwick-tree/FenwickTree.js#L6-L42","documentation":"FenwickTree.increase(position, value) adds value at a 1-based position and propagates it through the internal array using i += (i & -i). Valid positions are exactly 1..arraySize (the size given to the constructor); anything below 1 or above arraySize throws immediately. The guard exists because an out-of-range position would otherwise silently corrupt the treeArray indices shared by increase and query.","triggerScenarios":"Calling increase(0, v) because your source array is 0-indexed; calling increase(n, v) where n equals the element count but the tree was built with a smaller size; passing a computed index such as right + 1 that overflows arraySize; passing undefined or NaN which compares out of range.","commonSituations":"Wrapping a Fenwick tree over a 0-based array without adding 1; porting segment-tree code (typically 0-based) to a binary indexed tree; competitive-programming templates whose loop bounds were copied from a differently sized problem; constructing the tree with new FenwickTree(arr.length - 1) by mistake.","solutions":["Convert your 0-based index before calling: increase(i + 1, value).","If the failing index is above arraySize, re-check the size passed to new FenwickTree(n) — it must be at least your largest 1-based position.","Validate before calling: Number.isInteger(position) && position >= 1 && position <= tree.arraySize."],"exampleFix":"// before\nconst ft = new FenwickTree(arr.length);\nfor (let i = 0; i < arr.length; i += 1) {\n  ft.increase(i, arr[i]); // throws: position 0 is below 1\n}\n\n// after\nfor (let i = 0; i < arr.length; i += 1) {\n  ft.increase(i + 1, arr[i]); // 1-based position\n}","handlingStrategy":"validation","validationCode":"const isValidFenwickPosition = (position, tree) =>\n  Number.isInteger(position) && position >= 1 && position <= tree.arraySize;\n\nfunction safeIncrease(tree, position, value) {\n  if (!isValidFenwickPosition(position, tree)) {\n    throw new RangeError(`position must be in [1, ${tree.arraySize}], got ${position}`);\n  }\n  return tree.increase(position, value);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Adopt the tree's 1-based convention at one boundary: wrap FenwickTree in an adapter that accepts 0-based indices and adds 1 internally.","Assert new FenwickTree(n) is sized to your largest 1-based position before the first update.","Keep a single index-conversion helper instead of scattering '+ 1' through call sites.","Write one round-trip test (increase then query) with your real index convention to catch off-by-ones early."],"tags":["fenwick-tree","binary-indexed-tree","index-out-of-range","off-by-one","data-structures"],"backgroundTag":"array-index-out-of-range","analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}