Microsoft | SDE2 | Virtual Onsite
Anonymous User
1346
Aug 27, 2021
Sep 01, 2021

I was aksed this question -
There are n number of Bugs each bug has two attribute - days it take to solve it, days within it has to be solved. Find max number of bugs you can solve.
e.g [10, 15], [15, 25], [12, 16],
bug[0] take 10 days to fix and has to be fixed within 15 days from start. Similarily info for other bugs.
In this case, we can fix max 2 bugs as - [10, 15], [15, 25].

My solution was to sort the Bugs array and apply greedy algo. Default comparision operator will sort it in desired order. In the sorted output we will have shortest duration bug first. If two bugs have same shortest duration then they are sorted based on their days to finish. After that I greedily kept picking bug with lowest duration to fix and if current bug cannot be picked if we passed the cutoff days to fix it.
Interviewer was of the opinion it will not work in some cases. I thought about priority queue, but I am not able to find why greedy will not work here.

Edit:
Similar leetcode question is :- https://leetcode.com/problems/course-schedule-iii/

Comments (5)