{"record":{"id":"975ff3aa388c8c18","repo":"TheAlgorithms/JavaScript","slug":"index-out-of-bound","errorCode":null,"errorMessage":"Index Out of Bound","messagePattern":"Index Out of Bound","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Data-Structures/Array/QuickSelect.js","lineNumber":16,"sourceCode":"/**\n * [QuickSelect](https://www.geeksforgeeks.org/quickselect-algorithm/) is an algorithm to find the kth smallest number\n *\n * Notes:\n * -QuickSelect is related to QuickSort, thus has optimal best and average\n * -case (O(n)) but unlikely poor worst case (O(n^2))\n * -This implementation uses randomly selected pivots for better performance\n *\n * @complexity: O(n) (on average )\n * @complexity: O(n^2) (worst case)\n * @flow\n */\n\nfunction QuickSelect(items, kth) {\n  if (kth < 1 || kth > items.length) {\n    throw new RangeError('Index Out of Bound')\n  }\n\n  return RandomizedSelect(items, 0, items.length - 1, kth)\n}\n\nfunction RandomizedSelect(items, left, right, i) {\n  if (left === right) return items[left]\n\n  const pivotIndex = RandomizedPartition(items, left, right)\n  const k = pivotIndex - left + 1\n\n  if (i === k) return items[pivotIndex]\n  if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i)\n\n  return RandomizedSelect(items, pivotIndex + 1, right, i - k)\n}\n\nfunction RandomizedPartition(items, left, right) {","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Array/QuickSelect.js#L1-L34","documentation":"Thrown by QuickSelect when kth is less than 1 or greater than items.length. kth is a 1-based ordinal (the kth smallest element), not a 0-based index, so valid values are 1..items.length. It is a RangeError because the index is out of the valid range, distinct from a TypeError.","triggerScenarios":"Calling QuickSelect(arr, 0) (kth must be >= 1), QuickSelect(arr, arr.length + 1), QuickSelect([], 1) (empty array means length 0, so any kth > 0 fails), or QuickSelect(arr, -1).","commonSituations":"Treating kth as a 0-based array index (passing 0 for the first element); forgetting to handle the empty-array case; off-by-one when computing kth as items.length (that is the max, valid) vs items.length + 1 (invalid); passing a percentile-derived k without clamping.","solutions":["Remember kth is 1-based: for the smallest element pass 1, for the largest pass items.length.","Guard the empty-array case before calling: if (items.length === 0) return undefined.","Clamp kth: Math.max(1, Math.min(items.length, kth))."],"exampleFix":"// before\nQuickSelect(items, 0) // wanted smallest\n// after\nQuickSelect(items, 1) // 1-based: smallest element","handlingStrategy":"validation","validationCode":"if (!Array.isArray(items) || items.length === 0) {\n  throw new Error('items must be a non-empty array')\n}\nif (!Number.isInteger(kth) || kth < 1 || kth > items.length) {\n  throw new RangeError(`kth must be an integer in 1..${items.length}`)\n}\nQuickSelect(items, kth)","typeGuard":"const isValidKth = (items, kth) =>\n  Array.isArray(items) && items.length > 0 &&\n  Number.isInteger(kth) && kth >= 1 && kth <= items.length","tryCatchPattern":"try {\n  QuickSelect(items, kth)\n} catch (e) {\n  if (e instanceof RangeError && /Index Out of Bound/.test(e.message)) {\n    return QuickSelect(items, Math.max(1, Math.min(items.length, kth)))\n  }\n  throw e\n}","preventionTips":["Remember kth is 1-based: smallest = 1, largest = items.length.","Handle the empty-array case before calling.","Clamp kth derived from percentiles: Math.max(1, Math.min(items.length, kth))."],"tags":["validation","range-check","index","quickselect","data-structures","off-by-one"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}