PPractick

DSA interview question

Non-overlapping Intervals

Medium · Algorithms · Topics: greedy, array, sorting

The problem

A conference has more talk proposals than one stage can host, and two talks conflict if their time ranges overlap. Talks that merely touch, where one ends exactly when the next starts, are fine. What's the minimum number of talks to cancel so the rest don't overlap? That's n minus the maximum number of compatible talks, and the maximum comes from the classic activity-selection greedy: sort by end time and keep every talk that starts at or after the last kept talk ends.

Example: [[1,2], [2,3], [3,4], [1,3]] → 1 (remove [1,3]).

Function signature

int eraseOverlapIntervals(vector<vector<int>> intervals);

Test cases

InputOutputExplanation
[[1,2],[2,3],[3,4],[1,3]]1Remove [1,3]
[[1,2],[1,2],[1,2]]2Keep one copy
[[1,2],[2,3]]0Touching is allowed
[[1,100],[11,22],[1,11],[2,12]]2Keep [1,11] and [11,22]
[[0,2],[1,3],[2,4],[3,5],[4,6]]2Keep [0,2], [2,4], [4,6]

(Confirm: [a,b] and [b,c] don't overlap; the answer is a count of removals.)

How interviewers phrase it

Clarifying questions to ask

  1. Do intervals that share an endpoint overlap? (No.)
  2. Do I return the count or the removed intervals? (The count.)
  3. Is the input sorted? (No.)

Follow-ups to expect

  1. "Why is it the same as maximising the kept intervals?"
  2. "Why sort by end, not start?"
  3. "How is this related to the minimum number of arrows to burst balloons?"
  4. "Can you sort by start instead?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in