Find all intervals covering an integer

By | February 11, 2015
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

For example, we have intervals
[1,3]
[2,7]
[3,6]
[5,5]
[8,9]

If we give a number 3, it should return:
[1,3]
[2,7]
[3,6]

Solution 1, Linear solution
An obvious solution is to iterate all intervals. This takes O(n) time.

Solution 2, Range tree.
Maintain 2 BST for left and right bound.
 
Find the range for left bound and right bound, and do intersection