← Applied intuition Interview Insights
I started with the brute force since it's easier to explain and they seemed fine with that as a warmup.
Start by clarifying the problem and edge cases, then present the linear scan solution with O(n) time, followed by the binary search solution with O(log n) time. Emphasize the trade-offs and why binary search is preferred for large sorted arrays.
Pro tip: Mention that you can use two binary searches (one for first occurrence, one for last) or a modified binary search that finds the range. Also, note that the problem is essentially finding the lower and upper bounds of the target.
Ask if the array can be empty, if the target is guaranteed to be in the array, and if the array is sorted in ascending order. Confirm that duplicates are allowed.
Iterate through the array, record the first and last index where the target is found. If not found, return [-1, -1]. Time complexity O(n), space O(1).
Use binary search to find the first occurrence (leftmost) and last occurrence (rightmost) of the target. For first occurrence, when target is found, continue searching left; for last, continue searching right.
Discuss that linear scan is simple but O(n), while binary search is O(log n) and better for large arrays. Mention that binary search requires the array to be sorted.
Walk through an example like [1,2,2,2,3] with target 2 to show both approaches yield [1,3]. Also test edge cases like target not present or array of size 1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.