✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
You have a set of date intervals represented by StartDate and EndDate. How would you efficiently calculate the longest timespan covered by them?
What will be the time complexity?
The overall time complexity is O(NlogN) because:
Sort intervals by start date. Time complexity is O(NlogN).
Take first interval as actual range. Loop over intervals and if the current StartDate is within the actual range, extend EndDate of the actual range if needed and extend maximal timespan achieved so far if needed. Otherwise, use current interval as new actual range. Time complexity is O(N).