The Universal Framework for solving 5 Interval Problems | Greedy Algorithm
Mar 1, 2025
127 views
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
Imagine this: You’re in a coding interview at your dream company. The interviewer slides you a problem about scheduling meetings or merging overlapping intervals.
Your heart skips a beat this is one of the most common and trickiest topics in coding interviews. Why?
Because interval problems aren’t just about writing code; they’re about thinking strategically, handling edge cases, and optimising for efficiency.
And here’s the truth: if you’re not prepared for interval problems, you’re leaving one of the most frequently tested skills to chance. Let’s change that. 🚀
What Are Interval Problems?
Interval problems involve working with ranges defined by start and end points. These problems are used in real-world applications like:
A greedy algorithm makes the locally optimal choice at each step with the hope that these choices will lead to a globally optimal solution. It doesn’t look ahead or reconsider past decisions—it just focuses on the best immediate move.
Why Are Interval Problems Greedy?
Interval problems are greedy because they often involve making decisions based on local information (e.g., the current interval’s start or end time) that lead to an optimal global solution
Key Concepts to Master
Before diving into problems, understand these concepts:
1. Sorting Intervals:
2. Iterate Through the Intervals
for (let i = 1; i < intervals.length; i++) { const [startOfCurrent, endOfCurrent] = intervals[i]; if (startOfCurrent < endOfPrev) { // Handle overlap or conflict } else { // No overlap, update previous end time } }3. Handle Overlaps for conflicts:
if (startOfCurrent < endOfPrev) { count++; // For Non-overlapping Intervals endOfPrev = Math.min(endOfPrev, endOfCurrent); // Keep the smaller end } else { endOfPrev = endOfCurrent; // Update previous end time }1. Non-overlapping Intervals
Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.
/** * @param {number[][]} intervals * @return {number} */ var eraseOverlapIntervals = function (intervals) { // 1st sort intervals.sort((a, b) => a[0] - b[0]) let endOfPrev = intervals[0][1] let count = 0; //2nd iterate for (let i = 1; i < intervals.length; i++) { const [startOfCurrent, endOfCurrent] = intervals[i] //3rd compare if (startOfCurrent < endOfPrev) { count++ // cuase the min has less chance of conflict endOfPrev = Math.min(endOfPrev, endOfCurrent) } else { endOfPrev = endOfCurrent } } return count };2. Meeting Rooms
Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],...] (start_i < end_i), determine if a person could add all meetings to their schedule without any conflicts.
canAttendMeetings(intervals) { if (intervals.length < 2) { return true } intervals.sort((a, b) => a.start - b.start) let endOfPrev = intervals[0].end for (let i = 1; i < intervals.length; i++) { const {start: startOfCurrent,end: endOfCurrent} = intervals[i] if (startOfCurrent < endOfPrev) { return false } else { endOfPrev = endOfCurrent } } return true }3. Merge Intervals
Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
merge(intervals) { intervals.sort((a, b) => a[0] - b[0]); const output = [] output.push(intervals[0]) for (let i = 1; i < intervals.length; i++) { const endOfPrev = output[output.length - 1] const [startOfCurrent, endOfCurrent] = intervals[i] if (startOfCurrent <= endOfPrev[1]) { endOfPrev[1] = Math.max(endOfPrev[1], endOfCurrent) } else { output.push(intervals[i]) } } return output }4. Insert Interval
You are given an array of non-overlapping intervals intervals where intervals[i] = [start_i, end_i] represents the start and the end time of the ith interval. intervals is initially sorted in ascending order by start_i.
function mergeInterval(array) { array.sort((a, b) => a[0] - b[0]) const output = [] output.push(array[0]) for (let i = 0; i < array.length; i++) { const prev = output[output.length - 1] const endOfPrev = prev[1] const current = array[i] const startOfCurrent = current[0] if (startOfCurrent <= endOfPrev) { prev[1] = Math.max(endOfPrev, current[1]) } else { output.push(current) } } return output }insert(intervals, newInterval) { return mergeInterval([...intervals, newInterval]) }5. Meeting Rooms II
Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],...] (start_i < end_i), find the minimum number of days required to schedule all meetings without any conflicts.
minMeetingRooms(intervals) { const start = intervals.map(o => o.start).sort((a, b) => a - b) const end = intervals.map(o => o.end).sort((a, b) => a - b) let res = 0, count = 0, s = 0, e = 0 while (s < intervals.length) { if (start[s] < end[e]) { s++ count++ } else { e++ count-- } res = Math.max(res, count) } return res }#CodingInterviews #Algorithms #JavaScript #TechInterview #Programming #IntervalProblems