Instead of walking through your full time range by hops of 30 mn, you just have to hop from event to event (an event being "the start or end of a booking").
Thus you'll have a timeline of all your bookings starts and ends sorted together, with a counter that makes +1 when it encounters a start date and -1 for an end date. When the counter reaches n, go for the red flag!
Thus you'll have a timeline of all your bookings starts and ends sorted together, with a counter that makes +1 when it encounters a start date and -1 for an end date. When the counter reaches n, go for the red flag!