← BlackRock Interview Insights
Classic interval problem but I always forget to handle the edge cases cleanly under pressure.
Use a linear scan to handle three phases: intervals completely before the new interval, intervals that overlap and need merging, and intervals completely after. Maintain the sorted order by appending intervals in the correct sequence, merging overlaps by updating the new interval's start and end. This yields an O(n) time and O(n) space solution, which is optimal for this problem.
Pro tip: Clarify edge cases upfront, such as an empty list, the new interval being before all existing intervals, or after all of them. Also, mention that if the list were a balanced BST, insertion could be O(log n), but for an array-based list, O(n) is expected.
Restate the problem to ensure understanding: the input list is sorted and non-overlapping, and the output must also be sorted and non-overlapping. Ask about edge cases like empty list or intervals with equal boundaries.
Iterate through the list and add all intervals that end before the new interval starts. These intervals do not overlap and come before the new interval in sorted order.
While the current interval starts before or at the new interval's end, merge them by updating the new interval's start to the minimum of the two starts and its end to the maximum of the two ends.
Insert the merged new interval into the result, then add all remaining intervals that come after it. Return the result list.
State that the time complexity is O(n) and space complexity is O(n) for the output. Walk through a few test cases, including edge cases, to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.