{"record":{"id":"b6b806d3b2e9db18","repo":"TheAlgorithms/JavaScript","slug":"invalid-arguments","errorCode":null,"errorMessage":"Invalid arguments","messagePattern":"Invalid arguments","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Search/QuickSelectSearch.js","lineNumber":16,"sourceCode":"/*\n * Places the `k` smallest elements in `array` in the first `k` indices: `[0..k-1]`\n * Modifies the passed in array *in place*\n * Returns a slice of the wanted elements for convenience\n * Efficient mainly because it never performs a full sort.\n *\n * The only guarantees are that:\n *\n * - The `k`th element is in its final sort index (if the array were to be sorted)\n * - All elements before index `k` are smaller than the `k`th element\n *\n * [Reference](http://en.wikipedia.org/wiki/Quickselect)\n */\nexport function quickSelectSearch(array, k) {\n  if (!array || array.length <= k) {\n    throw new Error('Invalid arguments')\n  }\n\n  let from = 0\n  let to = array.length - 1\n  while (from < to) {\n    let left = from\n    let right = to\n    const pivot = array[Math.ceil((left + right) * 0.5)]\n\n    while (left < right) {\n      if (array[left] >= pivot) {\n        const tmp = array[left]\n        array[left] = array[right]\n        array[right] = tmp\n        --right\n      } else {\n        ++left\n      }","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Search/QuickSelectSearch.js#L1-L34","documentation":"Thrown by quickSelectSearch() when array is falsy or when k is greater than or equal to the array length. The algorithm places the k-th smallest element at its sorted index, so a valid k must be in [0, array.length-1]; k outside that range has no defined result. The guard also rejects null/undefined arrays since they have no length property.","triggerScenarios":"Calling quickSelectSearch([1,2,3], 3) (k equals length), quickSelectSearch([1,2,3], 5) (k exceeds length), quickSelectSearch(null, 0), or quickSelectSearch([], 0) with k=0 against an empty array (0 <= 0 triggers it).","commonSituations":"Computing k from array.length without subtracting for 0-indexing (e.g. k = arr.length); passing a percentile-derived k that rounds up to length; empty array from a filtered result with k=0.","solutions":["Ensure 0 <= k < array.length: clamp k to array.length - 1.","Handle the empty-array case explicitly before calling.","When asking for the top-k smallest, remember the function returns a slice, so pass k as the count minus one for the pivot index."],"exampleFix":"// before\nconst res = quickSelectSearch(arr, arr.length)\n\n// after\nconst k = Math.min(arr.length - 1, Math.max(0, desiredK))\nconst res = quickSelectSearch(arr, k)","handlingStrategy":"validation","validationCode":"function safeQuickSelectSearch(array, k) {\n  if (!Array.isArray(array) || array.length === 0) {\n    throw new Error('array must be a non-empty array')\n  }\n  if (!Number.isInteger(k) || k < 0 || k >= array.length) {\n    throw new RangeError(`k must be in [0, ${array.length - 1}]`)\n  }\n  return quickSelectSearch(array, k)\n}","typeGuard":"function isValidK(array, k) {\n  return Array.isArray(array) && array.length > 0 && Number.isInteger(k) && k >= 0 && k < array.length\n}","tryCatchPattern":"try {\n  quickSelectSearch(arr, k)\n} catch (e) {\n  if (e.message === 'Invalid arguments') {\n    return quickSelectSearch(arr, Math.min(k, arr.length - 1))\n  }\n  throw e\n}","preventionTips":["Always derive k with a clamp: Math.min(desiredK, array.length - 1).","Handle empty arrays before calling.","Treat k as a 0-based index, not a count."],"tags":["bounds-check","search","quickselect","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}