There were 2 parts to the questions:
A calender is given and you insert elements in the calender. and return in a sorted array on given conditions.
The calender elements look like - [startTime,endTime, key].
Example:
calendar.insert(5, 9, "c")
calendar.insert(5, 8, "b")
calendar.insert(5, 9, "a")
calendar.insert(6, 9, "c")Now the sorted array is returned as :
[ [5,8,b] , [6,9,c], [5,9,a] , [5,9,c]]
Here the sorting is done with the following conditions:
- The smallest endTime is returned first
- If endTimes are equal, then sorting based on ascending order of startTimes.
- If startTimes are equal, then sort based on key,
The time and space complexity was ask for the answer.
The follow up to this question was how would the algorithm change if the R:W ratio was 10:1 . This was something the interview seemed to have come up right then and there.
And the given ask was write as though you'd write it in production.
This is how I proceeded:
- I suggested quickSort strategy for incoming stream of events. - We would sort as soon as write happens because reading happens more often so time complexity should be cheaper for reads .
- I used the strategy design pattern.
I could have written tests but I didn't have time and couldn't get a working code even though the interviewer was happy with the logic. The structure of design (classes) seemed basic and fine to me.
Result: rejected


