Loading...
Loading...
You are building a hotel analytics dashboard for a travel platform. The platform receives a continuous stream of booking events, and your task is to determine the top 3 most frequently booked hotel categories from the event log.
Each booking event is a string representing a hotel category (e.g., "Luxury", "Budget", "Resort"). You must process all events and return the three categories with the highest booking counts in descending order of frequency. If two categories have the same count, they should be ordered alphabetically (ascending).
Input: A list of strings bookings where each string is a hotel category name.
Output: A list of up to 3 strings representing the top booked categories, ordered by frequency (descending). Ties broken alphabetically.
Input: ["Luxury", "Budget", "Luxury", "Resort", "Budget", "Luxury"]
Output: ["Luxury", "Budget", "Resort"]
Explanation:
Luxury → 3 bookingsBudget → 2 bookingsResort → 1 bookingTop 3 by frequency: Luxury, Budget, Resort.
Input: ["Budget", "Resort", "Spa", "Budget", "Resort", "Spa"]
Output: ["Budget", "Resort", "Spa"]
Explanation:
Budget, Resort, Spa.Input: ["Boutique"]
Output: ["Boutique"]
Explanation: Only one unique category exists. Return a list with just that one entry.
HashMap<String, Integer> to count occurrences of each category as you process the stream.min(3, list.size()) elements.PriorityQueue (min-heap of size 3) for an O(N log 3) = O(N) streaming approach, especially useful for very large inputs.0 <= bookings.length <= 10^5 1 <= bookings[i].length <= 50 bookings[i] consists of uppercase and lowercase English letters only The number of unique categories does not exceed bookings.length
Time Complexity: O(N + K log K) where N is number of bookings and K is number of unique categories. Space Complexity: O(K) for the frequency map.