Max Sum of Non-overlapping Intervals
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's OA in March is hitting candidates with max sum of non-overlapping intervals, and it's the kind of problem that feels obvious until you code it wrong. You're given a set of intervals, each with a value, and you need to pick a subset that doesn't overlap and maximizes the total sum. The pattern isn't immediately greedy. If you think 'just take the highest-value interval,' you'll miss the point. StealthCoder will catch you if you blank on the DP transition, which is where most candidates lose time.
Pattern and pitfall
The trick here is dynamic programming with interval scheduling. Sort intervals by end time, then for each interval, decide whether to include it or skip it. If you include interval i, you add its value to the best solution ending before i starts. You need a helper function (binary search works) to find the latest non-overlapping interval before the current one. The pitfall: candidates either greedy-grab high-value intervals without checking overlap, or they write an O(n-squared) solution that times out. The DP state is dp[i] = max sum considering intervals 0 to i. StealthCoder handles the recurrence logic live so you don't second-guess the transition or miss the binary search optimization during the OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Max Sum of Non-overlapping Intervals cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Max Sum of Non-overlapping Intervals FAQ
Is this really a dynamic programming problem or can I greedy it?+
Pure greedy fails. Counterexample: interval [1-5, value 10] vs. two intervals [1-3, value 6] and [4-5, value 7]. Greedy picks the first; DP picks both for sum 13. Sort by end time, then DP is the only safe play.
Do I need binary search to solve it, or will O(n-squared) pass?+
O(n-squared) will likely TLE on Amazon's test cases. Binary search to find the latest compatible interval is standard and drops you to O(n log n). Most candidates skip it and regret it. Do it.
What's the most common mistake on this problem?+
Forgetting to sort intervals by end time first. Without sorting, your DP indices don't align with non-overlapping logic. Also, using strict greater-than instead of greater-than-or-equal when checking overlap. Two intervals ending/starting at the same point don't overlap.
How do I check if two intervals overlap cleanly?+
Intervals [a, b] and [c, d] don't overlap if b less than c or d less than a. If you're iterating sorted by end time, you only need to check if the current interval's start is greater than the previous one's end. Keep a clean helper or inline it.
Should I memoize or build the DP table bottom-up?+
Bottom-up DP is clearer here and avoids recursion overhead. Build a 1D array where dp[i] tracks max sum up to interval i. No need for 2D. It's faster and easier to debug under pressure than top-down memoization.