Interview concept

What is binary search?

Binary search locates a value in sorted data by comparing the middle element to the target and discarding half the range each step, achieving O(log n) time.

Interview concept

How it works

Core loop

Track low and high bounds, compute a mid index, and move the bound that cannot contain the target.

Search on the answer

Many problems binary search over a range of possible answers, not just array indices.

Common pitfalls

Guard against overflow in mid calculation and off-by-one errors in loop bounds and termination.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When can I use binary search?

Use it when data is sorted or when the answer space is monotonic, so you can discard half the range each step.

What is the complexity of binary search?

Binary search runs in O(log n) time and O(1) extra space in its iterative form.