← Salesforce Interview Insights
Took me a minute to realize sorting by start day and using a min-heap on end days was the move.
Clarify that each event occupies exactly one day within its interval, so you need to select a maximum set of events with distinct assigned days. Sort events by end day and greedily assign each event to the earliest available day within its interval using a disjoint-set/union-find 'next available day' structure or a balanced tree.
Pro tip: Mention that if all events have the same duration, a simple greedy by end day works, but for arbitrary intervals the union-find approach is needed to avoid O(n^2) scanning. Also note the problem is equivalent to maximum bipartite matching between events and days, and the greedy is optimal due to interval scheduling matroid properties.
Ask about input size, day range, whether events can span multiple days, and if multiple events can share the same day. Confirm that each event can be attended on any single day within its [start, end] interval.
Sort the events in ascending order of their end day. This ensures that when processing an event, we prioritize those that expire soonest, which is key for the greedy choice.
Use a disjoint-set (union-find) structure where each day points to the next available day. For an event [s, e], find the earliest available day >= s; if it is <= e, assign it and union that day with day+1.
Increment a counter each time an event is successfully assigned to a day. If no available day exists within the interval, skip the event.
Explain that sorting takes O(n log n) and each union-find operation is nearly O(1), giving O(n log n) overall. Mention that a balanced BST (e.g., TreeSet) can also be used, and that the problem is a special case of maximum bipartite matching.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.