LeetCode 702, Medium. Topics: Array, Binary Search, Interactive. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:
Problem
This is an <strong><em>interactive problem</em></strong>.
You have a sorted array of <strong>unique</strong> elements and an <strong>unknown size</strong>. You do not have an access to the array but you can use the <code>ArrayReader</code> interface to access it. You can call <code>ArrayReader.get(i)</code> that:
<ul>
<li>returns the value at the <code>i<sup>th</sup></code> index (<strong>0-indexed</strong>) of the secret array (i.e., <code>secret[i]</code>), or</li>
<li>returns <code>2<sup>31</sup> - 1</code> if the <code>i</code> is out of the boundary of the array.</li>
</ul>
You are also given an integer <code>target</code>.
Return the index <code>k</code> of the hidden array where <code>secret[k] == target</code> or return <code>-1</code> otherwise.
You must write an algorithm with <code>O(log n)</code> runtime complexity.
Examples
Constraints
1 <= secret.length <= 10^4
-10^4 <= secret[i], target <= 10^4
- All the integers of
secret are <strong>unique</strong>.
secret is sorted in a strictly increasing order.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026