LeetCode 34 Find First and Last Position of Element - Why Naive Fails

The trap

Find the target with one binary search, then expand left and right to find boundaries.

The expansion step is O(k)O(k) where kk is the number of occurrences. If the entire array contains the target, that's O(n)O(n).

The problem requires O(logn)O(\log n). You need a way to find each boundary directly.