Final challenge: get every detail right
After this: Trace probes, choose the loop condition, write first occurrence and search a rotated array.
This challenge has four steps, and each step is scored on its own. You will trace the probes of a search, choose the loop condition that makes a search correct, and type the line that finds the first copy of a value. The last step goes a little beyond the lessons: you will search a rotated array, which is a sorted array cut in two with the pieces swapped. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 4Trace the probes
Trace the probes
Binary search [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] for 12. Which values are probed, in order, and where does it end?
Choose the loop condition
hi starts at the last index and the moves are lo = mid + 1 and hi = mid − 1. Which loop condition makes this search correct for every target?
def binary_search(nums, target): lo, hi = 0, len(nums) - 1 while ______________: # ← which condition? mid = (lo + hi) // 2 if nums[mid] == target: return mid if nums[mid] < target: lo = mid + 1 else: hi = mid - 1 return -1static int binarySearch(int[] nums, int target) { int lo = 0, hi = nums.length - 1; while (______________) { // ← which condition? int mid = lo + (hi - lo) / 2; if (nums[mid] == target) return mid; if (nums[mid] < target) lo = mid + 1; else hi = mid - 1; } return -1;}Write first occurrence
Complete first_occurrencefirstOccurrence. After remembering a hit at mid, the search must carry on in the part of the range that could hold an earlier copy.
def first_occurrence(nums, target): lo, hi = 0, len(nums) - 1 found = -1 while lo <= hi: mid = (lo + hi) // 2 if nums[mid] == target: found = mid ______________________ # ← keep looking for an earlier copy elif nums[mid] < target: lo = mid + 1 else: hi = mid - 1 return foundstatic int firstOccurrence(int[] nums, int target) { int lo = 0, hi = nums.length - 1; int found = -1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] == target) { found = mid; ______________________ // ← keep looking for an earlier copy } else if (nums[mid] < target) { lo = mid + 1; } else { hi = mid - 1; } } return found;}Search a rotated array
[38, 56, 72, 91, 2, 5, 8, 12, 16, 23] is the lessons' row, cut in two with the pieces swapped. This is called a rotated array. On each pass, the code below finds out which half around mid is sorted, then checks whether the target lies between that half's first and last values. Search for 8: lo = 0, hi = 9, and mid = 4 holds 2. nums[lo] is 38, which is bigger than 2. Which half is sorted, and where does the search go?
def rotated_search(nums, target): lo, hi = 0, len(nums) - 1 while lo <= hi: mid = (lo + hi) // 2 if nums[mid] == target: return mid if nums[lo] <= nums[mid]: # the left half is sorted ... if nums[lo] <= target < nums[mid]: # ... and the target fits between its ends hi = mid - 1 else: lo = mid + 1 else: # otherwise the right half is sorted if nums[mid] < target <= nums[hi]: lo = mid + 1 else: hi = mid - 1 return -1
nums = [38, 56, 72, 91, 2, 5, 8, 12, 16, 23]print(rotated_search(nums, 8), rotated_search(nums, 38), rotated_search(nums, 100))static int rotatedSearch(int[] nums, int target) { int lo = 0, hi = nums.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] == target) return mid; if (nums[lo] <= nums[mid]) { // the left half is sorted ... if (nums[lo] <= target && target < nums[mid]) { // ... and the target fits between its ends hi = mid - 1; } else { lo = mid + 1; } } else { // otherwise the right half is sorted if (nums[mid] < target && target <= nums[hi]) { lo = mid + 1; } else { hi = mid - 1; } } } return -1;}
int[] nums = {38, 56, 72, 91, 2, 5, 8, 12, 16, 23};System.out.println(rotatedSearch(nums, 8) + " " + rotatedSearch(nums, 38) + " " + rotatedSearch(nums, 100));- Trace the probes of a search, choose the right loop condition, write first occurrence, and search a rotated array.