Skip to content
Optimal Lab

Practice · 9 problems

Binary Search

0 of 9 solved on this device

Watch it first · interactiveBinary search →Read the explainer · optimal-roundWhen to use it, and its look-alikes ↗
  1. 017Binary SearchGiven a sorted array of distinct integers `nums` and an integer `target`, return the index of `target`, or -1 if it is absent. Must run in O(log n) time.Easy
  2. 018Search in Rotated Sorted ArrayAn ascending sorted array of distinct integers was rotated at some unknown pivot. Given the rotated array `nums` and an integer `target`, return its index or -1. Must run in O(log n) time.Medium
  3. 019Median of Two Sorted ArraysGiven two sorted arrays `nums1` and `nums2` of sizes m and n, return the median of the combined sorted array. The overall run time must be O(log(m + n)).Hard
  4. 063First Bad VersionVersions 1..n were released in order, and every version after the first bad one is also bad. Given an API `isBadVersion(v)` returning a boolean, find the first bad version using the fewest possible API calls.Easy
  5. 064Find Minimum in Rotated Sorted ArrayAn ascending sorted array of unique elements was rotated an unknown number of times. Return its minimum element in O(log n) time.Medium
  6. 065Koko Eating BananasKoko eats bananas from `piles` at k bananas per hour: each hour she picks one pile and eats up to k from it, moving on if the pile is smaller. Return the smallest integer k that lets her finish all piles within `h` hours.Medium
  7. 107Find Peak ElementA peak element is strictly greater than its neighbors. Given an array `nums`, return the index of ANY peak. You may imagine nums[-1] and nums[n] as negative infinity. Must run in O(log n) time.Medium
  8. 108Time Based Key-Value StoreDesign a key-value store where set(key, value, timestamp) records a value, and get(key, timestamp) returns the value set at the largest timestamp <= the one given, or "" if none exists. Implement the TimeMap class.Medium
  9. 109Split Array Largest SumGiven an array `nums` and an integer `k`, split the array into k non-empty contiguous subarrays so that the LARGEST subarray sum is as small as possible. Return that minimized largest sum.Hard