Algorithms

Binary Search

mediumalgorithmsbinary-searcharray
0:00

Problem

Given a sorted array of integers `nums` and a `target`, return the index of `target` if found. Otherwise return -1. Your algorithm must run in O(log n) time.

Your solution
Test results
Click Run tests to check your solution against the test cases.

About this challenge

Binary Search is a medium algorithms coding challenge. It tests algorithmic thinking and complexity analysis — the kind of problem-solving that shows up constantly in technical interviews. Write your solution in the editor above, run it against the test cases, and get instant feedback — free, in your browser, no sign-up.

Show a hint & approach (spoiler)

Hint: Maintain lo and hi pointers. At each step compute mid = lo + ((hi - lo) >> 1) to avoid overflow.

Keep practicing

Browse all coding challenges, or build intuition for the underlying concepts with the algorithm visualizers and the developer cheatsheets.