Dataford
Interview Guides
Upgrade
All questions/Coding/Binary Search in Sorted Catalog

Binary Search in Sorted Catalog

Easy
Coding
ArraysHash TablesSearching

Problem

Problem

A product team at Stripe stores item IDs in a sorted list and needs fast lookup. Implement an efficient search algorithm that returns the index of a target value in a sorted integer array.

Formal Specification

Write a function that takes:

  • nums: a list of integers sorted in ascending order
  • target: an integer to search for

Return:

  • The index of target if it exists in nums
  • -1 if target is not present

You must solve this in O(log n) time.

Examples

Example 1

  • Input: nums = [-1, 0, 3, 5, 9, 12], target = 9
  • Output: 4
  • Explanation: nums[4] is 9, so return 4.

Example 2

  • Input: nums = [-1, 0, 3, 5, 9, 12], target = 2
  • Output: -1
  • Explanation: 2 does not appear in the array.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • nums is sorted in strictly increasing order
  • Use an algorithm with logarithmic time complexity

Examples

Example 1
Inputnums = [-1, 0, 3, 5, 9, 12], target = 9Output4WhyThe target `9` appears at index `4`.
Example 2
Inputnums = [-1, 0, 3, 5, 9, 12], target = 2Output-1WhyThe value `2` is not in the sorted array.
Example 3
Inputnums = [5], target = 5Output0WhyThe array has one element, and it matches the target.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • nums is sorted in strictly increasing order
  • An O(log n) solution is required

Function Signature

def binary_search(nums, target):

Problem

Problem

A product team at Stripe stores item IDs in a sorted list and needs fast lookup. Implement an efficient search algorithm that returns the index of a target value in a sorted integer array.

Formal Specification

Write a function that takes:

  • nums: a list of integers sorted in ascending order
  • target: an integer to search for

Return:

  • The index of target if it exists in nums
  • -1 if target is not present

You must solve this in O(log n) time.

Examples

Example 1

  • Input: nums = [-1, 0, 3, 5, 9, 12], target = 9
  • Output: 4
  • Explanation: nums[4] is 9, so return 4.

Example 2

  • Input: nums = [-1, 0, 3, 5, 9, 12], target = 2
  • Output: -1
  • Explanation: 2 does not appear in the array.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • nums is sorted in strictly increasing order
  • Use an algorithm with logarithmic time complexity

Examples

Example 1
Inputnums = [-1, 0, 3, 5, 9, 12], target = 9Output4WhyThe target `9` appears at index `4`.
Example 2
Inputnums = [-1, 0, 3, 5, 9, 12], target = 2Output-1WhyThe value `2` is not in the sorted array.
Example 3
Inputnums = [5], target = 5Output0WhyThe array has one element, and it matches the target.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • nums is sorted in strictly increasing order
  • An O(log n) solution is required

Function Signature

def binary_search(nums, target):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
Binary Search in Sorted ArrayEasyABinary Search in Sorted ArrayEasyBinary Search in Sorted ArrayEasy
Next question