Given a list of date ranges, find a date which occurs maximum times
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Given several inclusive date ranges, the goal is to find the date covered by the largest number of ranges. The naive solution checks every day in every range, which works for tiny inputs but becomes wasteful when ranges span months or years. A better approach is a sweep-line algorithm using boundary events.
The Key Idea: Add at the Start, Subtract After the End
Suppose a range covers every day from 2025-01-03 through 2025-01-07, inclusive. Instead of incrementing five separate dates, record two events:
- '
+1on2025-01-03' - '
-1on2025-01-08'
Then sort all events by date and scan from left to right, keeping a running total. Whenever the running total increases past the best value seen so far, the current date is the new answer.
This works because the running total represents how many ranges are active on that date.
A Runnable Python Solution
For the sample input, the answer is 2025-01-04 with coverage count 4.
Why This Is Better Than Expanding Every Day
A day-by-day frequency array is easy to understand, but it becomes expensive when the date span is large. If your earliest date is in January and your latest date is two years later, you would allocate and update many more days than necessary.
The sweep-line version scales with the number of ranges, not with the full size of the calendar interval. For n ranges, you create at most 2n event dates and then sort them.
That makes the time complexity roughly:
- '
O(n log n)for sorting event dates' - '
O(n)additional scanning work'
This is usually much better than touching every day in every range.
Handling Ties
If several dates share the same maximum count, you need a tie rule. The implementation above returns the earliest date where the maximum is reached because it updates the answer only when current > best_count.
If instead you wanted the latest such date, you could change the comparison rule or record all maximal segments during the scan.
Being explicit about ties is important in interview questions and production code alike.
What About DateTimes Instead of Dates
The same event idea works for timestamps too, but you must define the interval semantics carefully.
With plain dates, inclusive ranges are natural, so subtracting on the day after end is easy. With timestamps, you often switch to half-open intervals, meaning start <= t < end, to avoid ambiguity around seconds, milliseconds, and timezone conversions.
If the original problem really is about calendar days, use date instead of datetime. It keeps the logic clean.
Common Pitfalls
The most common mistake is forgetting that the end date is inclusive. If you subtract on end instead of end + 1 day, the result is off by one.
Another mistake is expanding every date when the date span is huge. That can turn an elegant range problem into a memory-heavy brute-force solution.
A third pitfall is ignoring invalid input where end < start. Detect it early and fail clearly.
Summary
- Use a sweep-line algorithm instead of counting every day in every range.
- Record
+1at each start date and-1on the day after each inclusive end date. - Sort the event dates and scan with a running total.
- Decide how ties should be handled, usually earliest date first.
- Use
datefor calendar-day problems and switch carefully if the input is timestamp-based.
Related reading
- Given a list of dictionaries, how can I eliminate duplicates of one key, and sort by another
- Given a list of numbers and a number k, return whether any two numbers from the list add up to k
- Given a node, how long will it take to burn the whole binary tree?
- Given a permutation''s lexicographic number, is it possible to get any item in it in O1
- Given a set of points, how do I find the two points that are farthest from each other?
- Given a set of rectangles, do any overlap?
- Given a sorted array, can we build a sorted array of the sums of all pairs in On2?
- Given a sorted integer array, how may Binary Search trees can be formed from it?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.