PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Amazon

Implement binary search lower/upper bounds

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of binary search algorithms, handling of boundary conditions and duplicates, and the ability to implement efficient index-finding functions on sorted arrays within the Coding & Algorithms domain.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Machine Learning Engineer

Implement binary search lower/upper bounds

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a non-decreasing sorted array nums and a target value, implement two functions using binary search: ( 1) lower_bound(nums, target) that returns the smallest index i such that nums[i] >= target (return len(nums) if no such index exists); ( 2) upper_bound(nums, target) that returns the smallest index i such that nums[i] > target (return len(nums) if no such index exists). Achieve O(log n) time and O( 1) extra space, and handle duplicates, empty arrays, and targets outside the array’s value range. Clearly explain your boundary conditions and provide a few test cases to demonstrate correctness.

Quick Answer: This question evaluates a candidate's understanding of binary search algorithms, handling of boundary conditions and duplicates, and the ability to implement efficient index-finding functions on sorted arrays within the Coding & Algorithms domain.

Related Interview Questions

  • Find Unique Target-Sum Pairs - Amazon (easy)
  • Find Valid IP Addresses in Files - Amazon (medium)
  • Implement Optimal Bucket Batching - Amazon (hard)
  • Implement Cache and Rotate Matrix - Amazon (medium)
  • Find Longest Activatable Server Streak - Amazon (hard)
Amazon logo
Amazon
Sep 6, 2025, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
2
0

Given a non-decreasing sorted array nums and a target value, implement two functions using binary search: (

  1. lower_bound(nums, target) that returns the smallest index i such that nums[i] >= target (return len(nums) if no such index exists); (
  2. upper_bound(nums, target) that returns the smallest index i such that nums[i] > target (return len(nums) if no such index exists). Achieve O(log n) time and O(
  3. extra space, and handle duplicates, empty arrays, and targets outside the array’s value range. Clearly explain your boundary conditions and provide a few test cases to demonstrate correctness.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Machine Learning Engineer•Amazon Machine Learning Engineer•Amazon Coding & Algorithms•Machine Learning Engineer 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.