Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Greedy proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Make locally optimal choices only when an exchange or invariant proves global correctness. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Greedy when the prompt's constraints and required operations match this shape: Make locally optimal choices only when an exchange or invariant proves global correctness.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A greedy algorithm commits to one locally best option and never revisits it. The implementation is easy; proving that commitment preserves a global optimum is the real work.
Show that any optimal solution can replace its first differing choice with the greedy choice without becoming worse. Repeating that exchange transforms an optimum into the greedy solution.
def interval_choice_trace(intervals):
trace=[]; last_end=None
for interval in sorted(intervals,key=lambda item:(item[1],item[0])):
accepted=last_end is None or interval[0]>=last_end
trace.append([interval.copy(),accepted])
if accepted:last_end=interval[1]
return traceImplement interval_choice_trace(intervals). Sort by end and return [interval,accepted] decisions for non-overlapping selection.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Sorting exposes the choice order required by the proof. For interval scheduling, earliest finish leaves at least as much room for every future interval as any later finish.
def max_compatible_intervals(intervals):
count=0;last_end=None
for start,end in sorted(intervals,key=lambda item:(item[1],item[0])):
if last_end is None or start>=last_end:count+=1;last_end=end
return countImplement max_compatible_intervals(intervals). Return the maximum count of non-overlapping half-open intervals.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
A plausible local score is insufficient. If choices change future value in ways the exchange cannot preserve, use dynamic programming, search, or backtracking. Test a small counterexample before committing.
Say: “I choose the earliest finish. Given any optimal schedule, exchanging its first interval for mine cannot reduce remaining room, so an optimum with my choice exists.” Count sorting plus the linear scan and state endpoint semantics.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Greedy proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Greedy complete workflow | O(n log n) | O(n log n) | Sort by finish time and accept each interval whose start is at least the previous accepted end. |
O(n) for the focused Select the Most Non-Overlapping Intervals implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: An exchange argument, cut property, or maintained invariant proves the locally best legal choice is safe.
Avoid
def maximum_non_overlapping(intervals):
passUse instead
def maximum_non_overlapping(intervals):
count = 0
last_end = None
for start, end in sorted(intervals, key=lambda interval: (interval[1], interval[0])):
if last_end is None or start >= last_end:
count += 1
last_end = end
return countWhere you will hit this: Select the Most Non-Overlapping Intervals(opens in a new tab)
Sorts by start and accepts a long interval that blocks shorter compatible choices.
Prevent it: Preserve this proof obligation: Any optimal solution can be exchanged to include the chosen item without becoming worse.
Avoid
def maximum_non_overlapping(intervals):
count=0; end=None
for start,stop in sorted(intervals):
if end is None or start>=end: count+=1; end=stop
return countUse instead
def maximum_non_overlapping(intervals):
count = 0
last_end = None
for start, end in sorted(intervals, key=lambda interval: (interval[1], interval[0])):
if last_end is None or start >= last_end:
count += 1
last_end = end
return countWhere you will hit this: Select the Most Non-Overlapping Intervals(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(n log n).
Avoid
def maximum_non_overlapping(intervals):
count=0; end=None
for start,stop in sorted(intervals):
if end is None or start>=end: count+=1; end=stop
return countUse instead
def maximum_non_overlapping(intervals):
count = 0
last_end = None
for start, end in sorted(intervals, key=lambda interval: (interval[1], interval[0])):
if last_end is None or start >= last_end:
count += 1
last_end = end
return countWhere you will hit this: Maximum Container Area(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27