DEV Community

loading...
Cover image for Interviewcake - HiCal Solution in Golang

Interviewcake - HiCal Solution in Golang

mawulijo profile image friendlybytes ・1 min read

Your company built an in-house calendar tool called HiCal. You want to add a feature to see the times in a day when everyone is available.

To do this, you’ll need to know when any team is having a meeting. In HiCal, a meeting is stored as an object of a Meeting class with integer variables startTime and endTime. These integers represent the number of 30-minute blocks past 9:00am.

Breakdown

What if we only had two ranges? Let's take:

  [Meeting(1, 3), Meeting(2, 4)]
Enter fullscreen mode Exit fullscreen mode

These meetings clearly overlap, so we should merge them to give:

  [Meeting(1, 4)]
Enter fullscreen mode Exit fullscreen mode

But how did we know that these meetings overlap?

We could tell the meetings overlapped because the end time of the first one was after the start time of the second one! But our ideas of "first" and "second" are important here—this only works after we ensure that we treat the meeting that starts earlier as the "first" one.

How would we formalize this as an algorithm? Be sure to consider these edge cases:

The end time of the first meeting and the start time of the second meeting are equal. For example: [Meeting(1, 2), Meeting(2, 3)]
The second meeting ends before the first meeting ends. For example: [Meeting(1, 5), Meeting(2, 3)]

Here is my solution in Go

Discussion

pic
Editor guide