Uber | Phone | Minimum numbers to cover all intervals

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2019-12-01Added By Anonymous
Source: Link

Company: Uber

Interview Stage: Phone Screen


Original Post Content:


Given a list of tuples consisting of interval start and end time (both inclusive), find a set of numbers with minimum size such that at least k numbers from each interval are covered. Start by choosing k = 2. You can assume each interval will have size at least k.


Examples for k = 2:


[[1,4], [5,6]] - [1,4,5,6]

[[1,4], [3,6], [2,4]] - [3,4]

[[1,2], [2,3], [2,5]] - [1,2,3]

[[1,10], [3,7], [5,6]] - [5,6]

[[1,5], [4,7], [6,8], [9,13]] - [4,5,6,7,9,13]

[[1,10], [5,7], [7,13]] - [6,7,8]


I used a modification of interval intersection to identify common elements and propagate it through the sorted list. Was able to generate a linear solution for k=2 (if the list is already sorted, otherwise nlogn). Did not have sufficient time for generic k. Interviewer was surprised that my clumsy code worked for all test cases that he had. I guess that acted as a savior to not get rejected and have been called for another phone screen.

Comments