Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Binary Search High frequency Solution Available Practice Mode

Binary Search LeetCode Solution

Tags: #Array #Binary Search

Approach Summary

Classic binary search. Use mid = left + floor((right - left) / 2) to avoid overflow. While left <= right.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Binary Search Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Binary Search pattern.

Framework: Binary Search • Interview Prep: Tested at FAANG • Explore all Binary Search problems & invariants →

How to Recognize This Pattern

  • Sorted array
  • Find exact target

Complexity Analysis

Time Complexity

O(log n)

Space Complexity

O(1)

Tags

Array Binary Search

Frequently Asked Questions

What is the optimal time and space complexity for Binary Search?
The optimal solution for Binary Search achieves O(log n) runtime complexity and requires O(1) auxiliary space. Each iteration halves the range, so the number of iterations is log₂(n) — O(log n) time. Only constant extra space for three integers — O(1).
How do I identify that Binary Search requires the Binary Search pattern?
In technical interviews, look for key problem signals: Sorted array; Find exact target. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Binary Search strategy.
What critical edge cases must be handled in Binary Search?
Essential edge cases to verify include: Target smaller than every element — lo keeps moving right, ends with -1; Target at index 0; Target at the last index; Empty array or single-element array. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Binary Search?
Binary Search is a Easy-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google, Facebook.
Buy me a coffee
Daily Practice
0/0