PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Microsoft

Implement lower_bound on unknown-size sorted array

Last updated: Mar 29, 2026

Quick Overview

This question evaluates ability to design efficient search algorithms for unknown-length non-decreasing arrays accessed via a limited get(i) API, emphasizing correctness proof and tight query/time complexity bounds.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Data Scientist

Implement lower_bound on unknown-size sorted array

Company: Microsoft

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Given a non-decreasing integer array A of unknown length, accessible only via API get(i) that returns +∞ for i ≥ n, implement lower_bound(target): the smallest index i with A[i] ≥ target, or −1 if none exists. Use exponential search to bracket n and then binary search to find the answer with overall O(log n) time. Handle duplicates, empty array, all values < target, and all values ≥ target. Prove correctness, give a tight upper bound on the number of get calls, and explain how your approach changes if A may be rotated once (with or without duplicates).

Quick Answer: This question evaluates ability to design efficient search algorithms for unknown-length non-decreasing arrays accessed via a limited get(i) API, emphasizing correctness proof and tight query/time complexity bounds.

Related Interview Questions

  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Implement SFT Sample Packing - Microsoft (medium)
  • Implement SQL Table and DNA Ordering - Microsoft (medium)
  • Solve power jumps and graph tour - Microsoft (hard)
Microsoft logo
Microsoft
Oct 13, 2025, 9:49 PM
Data Scientist
Onsite
Coding & Algorithms
2
0

Lower Bound in Unknown-Length Nondecreasing Array via API

Setup

You are given a non-decreasing integer array A of unknown length n. You cannot access A directly; instead, you have an API:

  • get(i): returns A[i] for 0 ≤ i < n; returns +∞ for all i ≥ n.

We define lower_bound(target) as the smallest index i such that A[i] ≥ target. If no such i exists, return −1.

Task

Implement lower_bound(target) with the following constraints and deliverables:

  1. Algorithm
    • Use exponential search to bracket the answer, then binary search to find the exact index.
    • Overall time must be O(log n) and the number of get calls should also be O(log n).
  2. Edge cases to handle
    • Duplicates in A.
    • Empty array (n = 0).
    • All values in A < target.
    • All values in A ≥ target.
  3. Analysis
    • Prove correctness (why the algorithm returns the smallest index with A[i] ≥ target or −1 correctly).
    • Give a tight upper bound on the number of get calls (as a function of n and, where natural, of the answer index).
  4. Variant
    • Explain how your approach changes if A is non-decreasing but may be rotated once (e.g., [4,5,6,1,2,3]), both without and with duplicates.

Solution

Show

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Data Scientist•Microsoft Data Scientist•Microsoft Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.