Verified Jobs Live
Data Structures & Algorithms Medium Level ~3 min read

Implement binary search that returns an index or -1 in a sorted ascending array. What are its assumptions?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

Maintain a search interval and discard the half that cannot contain the target after each comparison. The input must be sorted according to the same comparison rule used by the search. The example retu s any matching index when duplicates exist; finding the first match requires a lower-bound variant. Empty input correctly retu s -1. Runtime is O(log n) and additional space is O(1). Test targets at both ends, a missing value between elements, duplicates, and an empty array.