Toppr Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
Create a binary search tree which stores the end points of each time interval. Each node should also store what time interval its value comes from.
Notation: dates are represented in MMDD format.
Nodes: (1010, 0), (1015, [0, 2]), (1012, 1), (1014, 1), (1024, 2), (1023, 3), (1103, 3), (1004, 4), (1005, 5).
After constructing this BST, for each time interval, perform a search looking for a value that is contained in the given interval AND does not belong to the given interval. These searches should be directed towards branches that may lead to time conflicts, i.e. if we got the interval [1015, 1024] and we face a node with value 1010, naturally we need to conduct the search to the branch in the right.
Assuming that we have the ability to create a balanced BST, the construction phase is O(nlogn) and the search phase is also O(nlogn).
While my solution seems to work for this problem, it is not correct for the general case (in which you pick just one interval and wants to get the conflicts for it). This is because my algorithm doesn't cover the case where an interval completely contains another one, e.g. [1000, 1005] and [1001, 1004].
Thus, you'd better use Interval Trees, which are 100% correct.
Use KD BST tree for storing these events and perform the range search. It gurantees to be O(nlogn) times in worst case can go upto n.
- Deep October 10, 2014