Adobe Interview Question
Data EngineersCountry: India
Interview Type: Phone Interview
depends what you mean exactly by "merge", e.g.
a: [1..7]
b: [5..10]
c: [9..13]
and you want [1..13] as a merged interval of a,b,c
you might want to sort by start times if you want to merge starting by start. You walk through the sorted intervals, pick the first and if the next overlaps you max on the end until no further overlap with maxed end occurs...
If you like thinking backwards, just do the same from the end with minimized start. Its symetric... so it seems to not matter on the result and run time.
If you want to work a bit more with your intervals, it may make sense to put them into an interval tree. you may google it.
It does not matter. The start time and the end time of intervals are in equal position for this problem.
e.g.
[1, 5], [2, 10], [6, 9] is currently sorted by start. You may merge them from left to right and get the result [1,10].
If they were sorted by end time instead - [1, 5], [6, 9],[2, 10]. You could merge them from right to left and get the same result [1, 10].
It just depends on if you wanna start the merge from the left or right side of the given inteval list.
A sample code for when intervals sorted by start time
Looking for interview experience sharing and coaching?
- aonecoding July 31, 2017Visit aonecode.com for private lessons by FB, Google and Uber engineers
Our ONE TO ONE class offers
SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.
Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.
Feel free to email us aonecoding@gmail.com with any questions. Thanks!