Closest Room - Problem

You're managing a luxury hotel booking system where guests have specific room preferences. Your hotel has n rooms, each with a unique room number and size. You need to efficiently handle k guest queries, where each guest specifies their preferred room number and minimum room size requirement.

For each query, find the room that:

  • Has a size of at least the guest's minimum requirement
  • Has a room number closest to their preferred number
  • In case of ties in distance, choose the room with the smaller room number

If no suitable room exists, return -1 for that query.

Example: If rooms are [[2,130],[3,120],[4,110]] and a guest queries [3,125], only room 2 meets the size requirement (130 ≥ 125), so return 2.

Input & Output

example_1.py — Basic Hotel Query
$ Input: rooms = [[2,130],[3,120],[4,110]], queries = [[3,125]]
Output: [2]
💡 Note: Guest prefers room 3 with minimum size 125. Only room 2 (size 130) meets the requirement. Distance from 3 to 2 is |2-3| = 1.
example_2.py — Multiple Queries
$ Input: rooms = [[1,100],[2,200],[3,150]], queries = [[2,100],[2,180]]
Output: [2,2]
💡 Note: Query 1: Rooms 1,2,3 all meet size 100. Room 2 is exact match for preferred 2. Query 2: Only rooms 2,3 meet size 180. Room 2 (distance 0) is closer than room 3 (distance 1).
example_3.py — No Valid Room
$ Input: rooms = [[1,50],[2,60]], queries = [[3,100]]
Output: [-1]
💡 Note: No room has size ≥ 100, so return -1 for this query.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ k ≤ 104
  • 1 ≤ roomIdi, preferredj ≤ 107
  • 1 ≤ sizei, minSizej ≤ 107
  • All room IDs are unique

Visualization

Tap to expand
Closest Room - Hotel Booking System INPUT Hotel Rooms Room #2 Size: 130 Room #3 Size: 120 Room #4 Size: 110 rooms array: [[2,130],[3,120],[4,110]] Guest Query Preferred Room: #3 Min Size Required: 125 queries array: [[3, 125]] ALGORITHM STEPS 1 Filter by Size Keep rooms with size >= 125 #2:130 OK #3:120 X #4:110 X 2 Valid Rooms Set Only Room #2 qualifies Valid: { Room #2 (size 130) } 3 Calculate Distance |preferred - room_num| |3 - 2| = 1 Distance from preferred #3 4 Select Closest Room #2 is only option Selected: Room #2 FINAL RESULT Query Result Assigned Room #2 Why Room #2? Size 130 >= 125 required Only valid room available Distance from #3 = 1 Output Array: [2] OK - Guest assigned successfully! Key Insight: The optimal approach uses sorting + binary search. Sort rooms by size descending, then process queries sorted by minSize descending. Maintain a TreeSet of valid room numbers for O(log n) closest lookup. This achieves O(n log n + k log k) time complexity instead of O(n * k) brute force approach. TutorialsPoint - Closest Room | Optimal Solution (Sorting + Binary Search)
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
23.6K Views
Medium-High Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen