← Pinterest Interview Insights
Spent the first couple minutes thinking about it wrong.
Recognize this as the classic interval scheduling maximization problem and propose a greedy solution: sort tasks by end time, then iterate through tasks, selecting each task that starts at or after the end of the last selected task. This yields the maximum number of non-overlapping tasks in O(n log n) time.
Pro tip: Mention that this greedy strategy is provably optimal and briefly explain the exchange argument: any optimal solution can be transformed to include the earliest-finishing task without reducing the count. This shows depth beyond just coding the solution.
Confirm that tasks cannot overlap and that each task runs on a single machine, so we need the maximum subset of non-overlapping intervals. Ask if intervals are inclusive/exclusive or if zero-length tasks are allowed.
Recognize this as the interval scheduling maximization problem, which is solvable with a greedy approach. Contrast with other interval problems (e.g., merging intervals) to show you understand the distinction.
Sort tasks by end time ascending. Initialize last_end = -infinity and count = 0. For each task in sorted order, if task.start >= last_end, select it, update last_end = task.end, and increment count.
State that sorting takes O(n log n) and the single pass takes O(n), so overall O(n log n) time and O(1) extra space (if sorting in place). Explain why greedy works: the earliest finishing task leaves the most room for subsequent tasks.
Apply the algorithm to [[1,3],[1,5],[4,6]]: sort by end time -> [[1,3],[1,5],[4,6]]. Select [1,3] (last_end=3). Next [1,5] starts at 1 < 3, skip. Next [4,6] starts at 4 >= 3, select (last_end=6). Count=2.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.