1. Empty input
Empty arrays cannot contain the target.
if (arr.length === 0) return -1Typorithm · medium
Step 1 of 8 · intro card
Search Algorithms
Exponential Search first finds a likely sorted range by doubling, then runs Binary Search inside that range.
Exponential Search works on sorted arrays.
It quickly expands the right boundary by powers of two.
Once the boundary reaches or passes target, the target can only be in the previous-to-current window.
The final phase is ordinary Binary Search inside that window.
Check empty and first item.
Double bound while values are too small.
Search between previous bound and current bound.
Uses Binary Search after range finding.
Time is O(log n).
function exponentialSearch(arr, target) {
if (arr.length === 0) return -1;
if (arr[0] === target) return 0;
let bound = 1;
while (bound < arr.length && arr[bound] < target) {
bound *= 2;
}
let left = Math.floor(bound / 2);
let right = Math.min(bound, arr.length - 1);
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}Connect each part of the code to the concept before you type.
Empty arrays cannot contain the target.
if (arr.length === 0) return -1Check index 0 before doubling from bound 1.
if (arr[0] === target) return 0Start with a small right bound and grow exponentially.
let bound = 1Double until the bound passes target or the array ends.
bound *= 2The target, if present, must be after the previous bound.
let left = Math.floor(bound / 2)Clamp the right boundary to the last valid index.
let right = Math.min(bound, arr.length - 1)After finding a window, use Binary Search inside it.
const mid = Math.floor((left + right) / 2)Read 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