Friday, March 27, 2015

Filter dates in a date range

Problem
We need to come with a list of dates where our service is online. Considering another list of date range(From through dates) where our service is down for maintenance.
We are looking for a date range of days, months, and may be years. So we might get a huge list of dates.
The 3 blocks on the image below represents down time of a service.
Solution
There are multiple ways to approach this problem but I need a smart way to implement it in O(1) or O(n) in the worst case.
Since I'm using C#. I can use a dictionary of datetime as a key and bool as It's value.
The dictionary keys will be all available dates with default value of true(service is available).
Since It's a dictionary, the insert operation is O(1).
Then we will go through each down time period and update the value of the Date key to true if the date is a key in our dictionary. 
Then we will return a list of Keys where the value is false.

Results
List of dates where our service is online and functioning.