Maximize profit from non-overlapping jobs
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates proficiency in weighted interval scheduling, dynamic programming concepts, and the use of efficient data structures for large-scale scheduling problems, including handling duplicate times, large reward values, and memory constraints.
Constraints
- 0 <= len(jobs) <= 200000
- 0 <= start < end <= 10^9 for every job
- 1 <= reward <= 10^9 for every job
- The total reward may exceed 32-bit signed integer range
Examples
Input: ([],)
Expected Output: (0, [])
Explanation: With no jobs, the maximum reward is 0 and the chosen set is empty.
Input: ([(1, 3, 50), (3, 5, 20), (6, 19, 100), (2, 100, 200)],)
Expected Output: (200, [3])
Explanation: Taking job 3 alone gives reward 200, which is better than chaining jobs 0, 1, and 2 for 170.
Hints
- After sorting by end time, let dp[i] be the best reward using the first i sorted jobs. For the i-th job, binary search the last job whose end time is <= its start time.
- Store both the compatible predecessor position and whether job i was taken. That lets you backtrack one optimal set of original indices. A segment tree with coordinate compression is an alternative O(N log N) approach; an online variant would need an ordered map or balanced tree keyed by end time.