Find First and Last Position
Recognize the pattern
Brute force idea
A straightforward first read of Find First and Last Position is this: Linear scan left to right for first, right to left for last. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
Better approach
The deeper shift in Find First and Last Position is this: Two binary searches: one finding the leftmost occurrence (left boundary), one finding the rightmost (right boundary). Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.
Key invariant
At the center of Find First and Last Position is one steady idea: Left boundary search: when nums[mid] == target, keep searching left (right = mid - 1). Right boundary search: when nums[mid] == target, keep searching right (left = mid + 1). When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Watch out for
A common way to get lost in Find First and Last Position is this: Using one binary search and then scanning — that degrades to when many duplicates exist. Two separate binary searches guarantee. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.