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
🏨 Smart Hotel Room AssignmentStep 1: Sort Rooms by Size[4,110][3,120][2,130]Step 2: Sort Queries by minSize (Descending)[3,125] first[1,110] secondStep 3: Binary Search ProcessQuery [3,125]: Valid rooms = [2] (size ≥ 125)🔍 Binary search for closest to 3: Room 2Distance: |2-3| = 1 ✓Query [1,110]: Valid rooms = [2,3,4]🔍 Closest to 1: Room 2 (distance 1)🎯 Key Optimization Benefits:• Process queries in smart order (largest minSize first)• Maintain sorted valid rooms for O(log n) lookup• Reuse valid rooms across multiple queries• Binary search eliminates linear scanning⚡ Complexity Analysis:Brute Force: O(k × n) - Check all rooms per queryOptimal: O(n log n + k log k + (n+k) log n)For n=10⁵, k=10⁴: 10⁹ → ~10⁶ operations!🚀 1000x faster for large inputs!
Understanding the Visualization
1
Initial Setup
Sort rooms by size and queries by minimum size requirement (descending)
2
Process High Requirements First
Start with queries needing largest rooms, building valid room set incrementally
3
Binary Search Magic
For each query, binary search the sorted valid rooms to find closest room number
4
Optimal Result
Return the best room for each guest with minimal computational overhead
Key Takeaway
🎯 Key Insight: By processing queries in descending order of size requirement and maintaining a sorted set of valid rooms, we transform an O(k×n) problem into an O(n log n) solution using the power of 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