The sorted part makes you think binary search immediately, but the unknown length is the actual problem.
Use exponential search to find a range where the target might exist, then binary search within that range. This minimizes get() calls by first doubling the index until get(high) >= k, then binary searching between low and high. Handle edge cases like empty array and target smaller than first element.
Pro tip: Explicitly discuss the trade-off between exponential and binary search, and mention that exponential search is optimal for unbounded arrays because it finds the range in O(log n) get() calls. Also, clarify that you assume get(i) returns a sentinel (e.g., infinity) for out-of-bounds indices, or handle it by catching exceptions.
Confirm with the interviewer how get(i) behaves for out-of-bounds indices (e.g., returns infinity or throws exception). Discuss edge cases: empty array, target smaller than first element, target larger than all elements.
Start with low = 0, high = 1. While get(high) < k, set low = high, high = high * 2. This finds a range [low, high] where k might be, using O(log n) get() calls.
Perform standard binary search between low and high (inclusive) to find the exact index of k. Use get(mid) to compare and adjust low/high accordingly.
If found, return the index; else return -1. Analyze time complexity: O(log n) get() calls, and space O(1). Mention that this is optimal for unbounded arrays.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.