1. Target in range
Only estimate while the target can be between the low and high values.
target >= arr[low] && target <= arr[high]Typorithm · hard
Step 1 of 8 · intro card
Search Algorithms
Interpolation Search estimates the target's position inside a sorted numeric range instead of always using the middle.
Interpolation Search works on sorted numeric data.
It estimates where the target should be based on low and high values.
This can be very fast when values are uniformly distributed.
It needs guards for out-of-range targets and equal low/high values.
Requires sorted numeric values.
Estimate position from value range.
Move boundaries around the estimate.
Fast on uniform data.
Can degrade on uneven distributions.
function interpolationSearch(arr, target) {
let low = 0;
let high = arr.length - 1;
while (low <= high && target >= arr[low] && target <= arr[high]) {
if (arr[low] === arr[high]) {
return arr[low] === target ? low : -1;
}
const position = low + Math.floor(
((target - arr[low]) * (high - low)) / (arr[high] - arr[low])
);
if (arr[position] === target) return position;
if (arr[position] < target) low = position + 1;
else high = position - 1;
}
return -1;
}Connect each part of the code to the concept before you type.
Only estimate while the target can be between the low and high values.
target >= arr[low] && target <= arr[high]Avoid dividing by zero when all remaining values are equal.
if (arr[low] === arr[high])Estimate where target should be based on its value within the current range.
const position = low + Math.floor(Return immediately if the estimated position is correct.
if (arr[position] === target) return positionIf the estimate is too small, search to the right.
low = position + 1If the estimate is too large, search to the left.
high = position - 1Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputArray [1,3,5,7,9,11,13,15] · target 9
Follow temporary values, local state and supporting structures at this step.
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state