The iterative part was fine, lower bound binary search, nothing too bad.
Start by clarifying the problem and edge cases, then implement iterative binary search to find the lower bound (first element >= target). Next, implement a recursive version, discuss time/space complexity, and finally explain how to adapt the logic to find the upper bound (last element <= target).
Pro tip: Emphasize that the iterative solution uses O(1) space while the recursive uses O(log n) due to call stack, and mention that in production you'd prefer the iterative version for efficiency. Also, note that the same binary search template can be easily modified for the upper bound by changing the comparison and update rules.
Restate the problem: find the first index where array[index] >= target. Discuss edge cases: empty array, all elements less than target, all elements greater than or equal to target, duplicates.
Implement binary search iteratively using low and high pointers. Maintain the invariant that the answer is in [low, high]. When array[mid] >= target, update high = mid; else low = mid + 1. Return low after loop.
Implement the same logic recursively with a helper function that takes low and high. Base case: low == high, return low. Recursive case: if array[mid] >= target, recurse on [low, mid]; else recurse on [mid+1, high].
Time complexity: O(log n) for both iterative and recursive. Space complexity: O(1) for iterative, O(log n) for recursive due to call stack. Mention that recursion depth is logarithmic.
To find the last element <= target, change the condition: when array[mid] <= target, update low = mid; else high = mid - 1. Be careful with infinite loops; use mid = (low + high + 1) // 2 to avoid rounding down.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.