Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Conflicting Appointments

Problem Statement

Given an array of intervals representing ‘N’ appointments, find out if a person can attend all the appointments.

Example 1:

Intervals: [[1,4], [2,5], [7,9]]
Output: false
Explanation: Since [1,4] and [2,5] overlap, a person cannot attend both of these appointments.

Example 2:

Intervals: [[6,7], [2,4], [13, 14], [8,12], [45, 47]]
Output: true
Explanation: None of the appointments overlap, therefore a person can attend all of them.

Example 3:

Intervals: [[4,5], [2,3], [3,6]]
Output: false

.....

.....

.....

Like the course? Get enrolled and start learning!
R

rseeto11

· 3 years ago

In the solution, an interval is implemented as a list of lists. In the problem, an interval is implemented as a class as in 'Merge Intervals'. Therefore, the solution provided does not work.

T N

T N

· 3 years ago

f

Show 4 replies
Mohammed Dh Abbas

Mohammed Dh Abbas

· 2 years ago

#class Interval: # def __init__(self, start, end): # self.start = start # self.end = end # def print_interval(self): # print("[" + str(self.start) + ", " + str(self.end) + "]", end='') class Solution: def canAttendAllAppointments(self, intervals): def do_overlap(a, b): return b.start < a.end # sort the intervals first to compare the overlapping intervals intervals.sort(key = lambda x: x.start) for i in range(1, len(intervals)): # check the interval and the previous interval for overlapping if do_overlap(intervals[i - 1], intervals[i]): return False return True
R

raphael_1st

· 2 years ago

If you want to flex in your Java interview, you can use the comparator like this as well,

Arrays.sort(intervals, Comparator.comparingInt(i->i.start));

N

neojw1505

· 3 months ago

this question should come before merge intervals, to build the idea