I was asked to design a data structure for the meeting schedules and after that to merge them. For example if person A has meeting from 9:00 AM to 10:00 AM and person B has meeting from 9:30 AM to 11:30 AM then the merged busy slot is from 9:00 AM to 11:30 AM.
I made the classes for the Person and this class has the collection of meeting objects. The Meeting class has the start time [hh:mm] in 24 hours format so that I can do the comparison easily.
class Person {
String name;
Collection<Meeting> meetings;
}
class Meeting{
int hh, mm;
int duration; // duration will be in minutes from where we can get the end time.
}
I want to know that which data structure will be most efficient for merging.
One way is to use the sorted ArrayList of meeting.
Any better design is appreciated.
As @Anonymouse suggested you can use 96 bits i.e. 12 bytes to represent a day so a 30 min meeting starting at 1:00 Am would be represented as 110000 and you can use simple | operation on all numbers.
Time O(n) Memory O(12n) byte. It would be way faster theoretically.
Given a Meeting [start time in minute, end time in minute].
Merging two meetings (Sa & Sb) into Sc when overlapping
Sc [ minimum (SA-start, SB-start), maximum (SA-end, SB-end) ] and storing merged meetings in collection. If not overlapping then you can store them separately.
We know that total minutes in a day = 24 * 60 = 1440
If you have 15 minute unit then it becomes 24 * 60 / 15 = 96 (under 1 byte)
So you need 2 byte per schedule i.e. byte start, end.
Time O(n) Memory O(2n) byte
Both approach won’t work if you have to delete a meeting later. For that you would definitely to hold all original meeting schedule separately.