DSA interview question
A meeting-room booking system exports each room's bookings as [start, end] time intervals, unsorted and often overlapping (people book over each other, or extend a meeting). The facilities team wants the consolidated busy periods: overlapping or touching bookings merged into single intervals. The key step is sorting by start time. After that, every interval either extends the current busy period or starts a new one, so a single scan finishes the job.
Example: [[1, 3], [2, 6], [8, 10], [15, 18]] → [[1, 6], [8, 10], [15, 18]].
vector<vector<int>> merge(vector<vector<int>> intervals); // merged, sorted by start
Input intervals | Output | Explanation |
|---|---|---|
[[1,3],[2,6],[8,10],[15,18]] | [[1,6],[8,10],[15,18]] | [1,3] and [2,6] overlap |
[[1,4],[4,5]] | [[1,5]] | Touching intervals merge |
[[1,4],[2,3]] | [[1,4]] | A contained interval disappears |
[[5,6],[1,2]] | [[1,2],[5,6]] | Unsorted input |
[[1,10],[2,3],[4,5],[11,12]] | [[1,10],[11,12]] | One long interval absorbs several |
[[1,1]] | [[1,1]] |
(Confirm: intervals are closed, so [1,4] and [4,5] overlap; the output is sorted by start.)
max for the end?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in