704. Binary Search
easyAsked at AkamaiSearch a sorted array for a target value in O(log n) time. Akamai uses binary search as a litmus for algorithmic correctness — the off-by-one bug in the loop condition is the same class of error that causes out-of-bound reads in high-performance C++ networking code.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Akamai loops.
- Glassdoor (2026-Q1)— Akamai interview reports note binary search variants appear as warm-ups in onsite algorithm rounds.
- Blind (2025-11)— Candidates report Akamai asks binary search to test precision under the 'explain before coding' expectation.
Problem
Given an array of integers nums sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.
Constraints
1 <= nums.length <= 10^4−10^4 < nums[i], target < 10^4All integers in nums are unique.nums is sorted in ascending order.
Examples
Example 1
nums = [-1,0,3,5,9,12], target = 94Explanation: 9 exists at index 4.
Example 2
nums = [-1,0,3,5,9,12], target = 2-1Explanation: 2 does not exist in nums.
Approaches
1. Iterative binary search
Maintain left and right pointers. Compute mid = left + (right - left) >> 1 (avoids integer overflow). Narrow the search range based on the comparison with target.
- Time
- O(log n)
- Space
- O(1)
function search(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = left + ((right - left) >> 1); // avoids overflow vs (left+right)/2
if (nums[mid] === target) return mid;
if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}Tradeoff: O(log n) time, O(1) space. The inclusive right boundary (right = nums.length - 1) and loop condition (left <= right) are the canonical correct form. Mention the overflow-safe midpoint calculation — Akamai C++ backgrounds mean interviewers notice.
Akamai-specific tips
State the invariant before coding: 'The target, if present, is always within [left, right]. I shrink the window by half each iteration, so the loop terminates in O(log n) steps.' Akamai interviewers consistently give credit for stating the invariant explicitly. Also mention the overflow-safe midpoint — it signals systems-level awareness.
Common mistakes
- Using (left + right) / 2 — overflows with large indices in languages with fixed-width integers. Always use left + (right - left) / 2.
- Loop condition left < right (exclusive) instead of left <= right — misses the single-element search space where target might live.
- Updating mid instead of mid + 1 or mid - 1 when shrinking — creates an infinite loop when left === right.
Follow-up questions
An interviewer at Akamai may pivot to one of these next:
- Search in Rotated Sorted Array (LC 33) — binary search with a twist.
- Find First and Last Position of Element in Sorted Array (LC 34) — two binary searches for left and right bounds.
- How would you binary search on a condition rather than a value (binary search on the answer)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why left <= right and not left < right?
When left === right, there is still one unchecked element. The <= condition ensures we check it before concluding the target is absent.
Why right = nums.length - 1 and not nums.length?
nums.length - 1 is the last valid index. With nums.length, the mid calculation could go out of bounds when left = 0 and right = nums.length.
Practice these live with InterviewChamp.AI
Drill Binary Search and other Akamai interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →