Problem, as an interviewer would state it:
"Implement binary search on a sorted array, returning the index of the target or -1. Discuss edge cases, runtime, and how you would handle duplicates."
Examples:
binarySearch([1, 3, 5, 7, 9, 11], 7); // 3
binarySearch([1, 3, 5, 7], 4); // -1
Clarifying questions expected:
- Is the array guaranteed sorted, and in which order and by what comparison?
- If the target appears several times, which index should be returned — any, the first, or the last?
- Should I return
-1on a miss, or the insertion point?
Code / implementation expected: Yes — real, direct proof against a brute-force reference, plus the duplicate-handling variants.