Your question is Binary Search and Edge Cases. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
GrabMart maintains item IDs in non-decreasing order for a local in-memory index. Given this sorted list and a target item ID, return the index of the first occurrence of the target. Return -1 if the target does not exist.
Implement an iterative binary search. Because duplicate IDs may appear, finding any matching index is not sufficient. When a match is found, record it and continue searching the left half.
item_ids, a list of integers sorted in non-decreasing order, and target, an integer.i such that item_ids[i] == target, or -1 when no such index exists.def find_first_item_index(item_ids, target):