This is a design round. You write the classes, and the interviewer checks that they behave.
Here you build Scheduler. It books the meeting rooms of an office. The rooms are numbered from 1 to rooms.
Implement the class:
Scheduler::new(rooms)starts with every room empty.book(start, end)books a meeting fromstarttoend. It returns the room it gets, or 0.cancel(room, start)removes the meeting inroomthat starts atstart. It returnstrueif there was one.is_free(room, time)returnstrueif no meeting inroomis going on attime.meetings(room)returns how many meetingsroomhas.
Rust has no classes. Scheduler is a struct, and its methods sit in an impl block. Its new function makes one.
Times are whole numbers. A meeting covers every time from start up to end, but not end itself. So a meeting from 10 to 20 is going on at 10 and at 19. At 20 it is over.
Two meetings in one room may not overlap. They may touch: one can end at 20 and the next can start at 20.
A new meeting goes to the lowest-numbered room that is free for its whole time. When no room is free, book returns 0 and books nothing.
Two meetings of one room never start at the same time. So a room and a start name one meeting. When there is no such meeting, cancel returns false and changes nothing.
The calls run in order on one new Scheduler. A call to book writes the room number. A call to cancel or is_free writes True or False. A call to meetings writes the number.
Example 1:
Input: Scheduler::new(2) 1. book(9, 12) 2. book(10, 11) 3. book(12, 14) 4. book(9, 10) 5. book(11, 13) 6. book(8, 15) 7. meetings(1) 8. meetings(2) Output: 1 2 1 2 2 0 2 3 Explanation: Call 1 puts a meeting from 9 to 12 in room 1. Call 2 overlaps it, so it goes to room 2. Call 3 starts at 12, when the first meeting ends. Room 1 fits. Call 4 overlaps room 1. It ends at 10, when the meeting in room 2 starts. So room 2 fits. Call 5 overlaps room 1 too. It starts at 11, when that meeting in room 2 ends. Call 6 overlaps a meeting in each room, so it returns 0. Room 1 now has 2 meetings, and room 2 has 3.
Example 2:
Input: Scheduler::new(1) 1. book(10, 20) 2. is_free(1, 10) 3. is_free(1, 19) 4. is_free(1, 20) 5. is_free(1, 9) 6. cancel(1, 15) 7. cancel(1, 10) 8. is_free(1, 15) 9. cancel(1, 10) 10. meetings(1) Output: 1 False False True True False True True False 0 Explanation: The meeting in room 1 runs from 10 up to 20. So the room is busy at 10 and at 19. It is free at 20, when the meeting is over. It is free at 9 too. No meeting starts at 15, so call 6 returns False. Call 7 removes the meeting. Then the room is free at 15. Call 9 finds nothing left to remove.
Example 3:
Input: Scheduler::new(3) 1. book(0, 100) 2. book(0, 100) 3. book(50, 60) 4. book(50, 60) 5. cancel(2, 0) 6. book(50, 60) 7. book(100, 200) 8. meetings(1) Output: 1 2 3 0 True 2 1 2 Explanation: Calls 1 and 2 fill rooms 1 and 2 from 0 to 100. Call 3 goes to room 3. Call 4 finds no free room. Call 5 removes the meeting in room 2. So call 6 gets room 2, the lowest room that is free. Call 7 starts at 100, when the meeting in room 1 ends. It gets room 1.
Constraints:
roomsis from 1 to 20.- Each
roomis from 1 torooms. - 0 <=
start<end<= 1,000,000,000 inbook. - In
cancelandis_free,startandtimeare from 0 to 1,000,000,000. - At most 100,000 calls follow the constructor.
Note:
Keep each room's meetings sorted by start. A binary search finds where a new meeting would go. It fits when the meeting before that place ends by start. The meeting after it must start at end or later. So book takes O(r log n) time for r rooms with n meetings each. Each call to cancel or is_free takes O(log n). Inserting into a sorted list or removing from it also moves the items after that place.
Free plan: 30 tutor messages. PRO is unlimited.
