← Grammarly Interview Insights
I knew the greedy approach here: sort by end time, walk through and count removals whenever the current start is before the last recorded end.
Reframe the problem as finding the maximum number of non-overlapping intervals, then subtract that from the total. Sort intervals by end time and greedily select intervals that start after the last selected end time. The minimum removals equals total intervals minus the maximum non-overlapping count.
Pro tip: Mention that sorting by end time is optimal for interval scheduling, and briefly explain why sorting by start time fails with a counterexample. This shows deeper understanding and avoids a common pitfall.
Confirm that intervals are closed and that overlapping means sharing any point. Reframe the problem: minimum removals = total intervals - maximum non-overlapping intervals.
Explain that to maximize non-overlapping intervals, we should always pick the interval that ends earliest and is compatible with previous choices. This is the classic interval scheduling greedy algorithm.
Sort all intervals in ascending order of their end times. This ordering ensures that we consider intervals that finish sooner first, leaving more room for subsequent intervals.
Initialize a variable to track the end time of the last selected interval (e.g., -infinity) and a count of selected intervals. For each interval in sorted order, if its start time is >= last end time, select it, update last end time, and increment count.
After iterating, the maximum number of non-overlapping intervals is the count. The minimum removals is total intervals minus this count. Return that value.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.