this question came in my onsite Amazon interview. Can someone come up with an optimizated solution? What data structure would you use to store the sessions?
Given the start and end time of a session, find the maximum number of sessions that were active at the same time.
Example:
ID. Start Time , End Time
1 , 4
3 , 5
2 , 7
5 , 10
Answer: 3
I was able to come up with a brute-force solution. I stored the sessions in list of lists [[s1,e1],[s2,e2]]. I ran a loop for each unit of time through each session in the list and calculated the maximum number of overlaps. Obviously, this is not efficient. What can I use for a more efficient solution? Would it be better to store the start and end times in two different arrays?
I have solved this problem using an unordered map and cumulative sum in O(n) time. My solution: