PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Amazon

Implement lower_bound and upper_bound binary search

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency with binary search, precise index and boundary management in sorted arrays, and the ability to reason about algorithmic correctness, loop invariants, and time/space complexity.

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

Implement lower_bound and upper_bound binary search

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement two functions for a nondecreasing sorted integer array nums of length n: lower_bound(target) returns the smallest index i such that nums[i] >= target, or n if no such index exists; upper_bound(target) returns the smallest index i such that nums[i] > target, or n if no such index exists. Provide iterative implementations with O(log n) time and O( 1) space, handle edge cases (empty array, all elements < or > target, duplicates, negatives), include tests, and briefly argue correctness and boundary invariants.

Quick Answer: This question evaluates proficiency with binary search, precise index and boundary management in sorted arrays, and the ability to reason about algorithmic correctness, loop invariants, and time/space complexity.

Related Interview Questions

  • Count Connected Components in an Undirected Graph - Amazon (medium)
  • 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)
Amazon logo
Amazon
Sep 6, 2025, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
2
0

Implement two functions for a nondecreasing sorted integer array nums of length n: lower_bound(target) returns the smallest index i such that nums[i] >= target, or n if no such index exists; upper_bound(target) returns the smallest index i such that nums[i] > target, or n if no such index exists. Provide iterative implementations with O(log n) time and O(

  1. space, handle edge cases (empty array, all elements < or > target, duplicates, negatives), include tests, and briefly argue correctness and boundary invariants.

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.