Random Point in Non-overlapping Rectangles - Problem

Imagine you're designing a random location generator for a video game where players can spawn at any integer coordinate within designated safe zones (rectangles). You need to ensure that every possible spawn point has an equal probability of being selected!

You're given an array of non-overlapping axis-aligned rectangles where each rectangle is defined as [a_i, b_i, x_i, y_i]:

  • (a_i, b_i) = bottom-left corner
  • (x_i, y_i) = top-right corner

Your task: Design an algorithm that can pick a random integer point inside the space covered by these rectangles. Points on the perimeter are included, and every integer point should be equally likely to be returned.

Example: If you have rectangles [[1,1,3,3], [2,2,4,4]], your algorithm should be able to return any integer coordinate like [1,1], [3,2], [4,4], etc., with proper probability distribution.

Input & Output

example_1.py โ€” Basic Rectangle Selection
$ Input: rects = [[1,1,3,3]] sol = Solution(rects) sol.pick() # Called multiple times
โ€บ Output: [2,1], [1,3], [3,2], etc.
๐Ÿ’ก Note: With only one rectangle from (1,1) to (3,3), there are 9 possible integer points: (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3). Each should have equal 1/9 probability.
example_2.py โ€” Multiple Rectangles
$ Input: rects = [[1,1,3,3], [2,2,4,4]] sol = Solution(rects) sol.pick() # Called multiple times
โ€บ Output: [1,1], [4,4], [2,3], etc.
๐Ÿ’ก Note: First rectangle has 9 points, second has 9 points, total 18 points. Each rectangle should be selected with probability proportional to its area (both equal here, so 50% each).
example_3.py โ€” Different Sized Rectangles
$ Input: rects = [[0,0,1,1], [1,0,3,0]] sol = Solution(rects) sol.pick() # Called multiple times
โ€บ Output: [0,0], [2,0], [1,1], etc.
๐Ÿ’ก Note: First rectangle: 2ร—2=4 points, second rectangle: 3ร—1=3 points. First rectangle should be selected ~57% of the time, second ~43% of the time.

Visualization

Tap to expand
Small Venue6ร—4 = 24 seatsLarge Venue10ร—6 = 60 seatsMedium5ร—3 = 15 seatsTotal: 99 tickets24 tickets60 tickets15 ticketsTicket DistributionRandom ticket #45 โ†’ Large VenuePick random seatโœ“ Fair Distribution: Probability โˆ Venue SizeSmall: 24/99 โ‰ˆ 24%Large: 60/99 โ‰ˆ 61%Medium: 15/99 โ‰ˆ 15%
Understanding the Visualization
1
Calculate Rectangle Areas
Each rectangle gets tickets equal to its area (number of integer points)
2
Build Cumulative Distribution
Create ranges for each rectangle proportional to their areas
3
Random Selection
Pick a random ticket number from the total pool
4
Binary Search
Find which rectangle owns that ticket number
5
Generate Point
Pick a random point within the selected rectangle
Key Takeaway
๐ŸŽฏ Key Insight: Use cumulative area distribution to ensure each integer point has equal probability, regardless of which rectangle it belongs to!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each pick operation takes constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only stores the input rectangles array

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค rects.length โ‰ค 100
  • rects[i].length == 4
  • -109 โ‰ค ai < xi โ‰ค 109
  • -109 โ‰ค bi < yi โ‰ค 109
  • xi - ai โ‰ค 2000
  • yi - bi โ‰ค 2000
  • All rectangles are non-overlapping
  • At most 104 calls will be made to pick
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
23.8K Views
Medium Frequency
~25 min Avg. Time
892 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