The Random Point in Non-overlapping Rectangles problem asks you to uniformly randomly pick a point in a set of non-overlapping axis-aligned rectangles. The point is picked with probability proportional to the rectangle's area. This coding problem combines weighted random selection with 2D point generation. The array, math, binary search, reservoir sampling, randomized, and prefix sum interview pattern is demonstrated.
Uber and Google ask this because it extends random pick with weight to 2D geometry. The key: the probability of picking each rectangle is proportional to its area, then uniformly sample within the chosen rectangle.
Area-weighted prefix sums + binary search + 2D sampling. Compute area of each rectangle. Build prefix area array. For each pick: random number in [1, total_area], binary search for the rectangle. Within the chosen rectangle (x1,y1,x2,y2): pick random x in [x1,x2], random y in [y1,y2].
rects=[(1,1,3,3),(5,5,8,7)]. Areas: 4, 6. Prefix: [4,10]. Total=10.
Random Point in Non-overlapping Rectangles combines area-weighted sampling with 2D point generation. The two-step process: (1) pick rectangle proportional to area, (2) uniform point within chosen rectangle — is the standard approach for any multi-region sampling problem. Practice extending this to 3D volumes, irregular shapes (triangles), or dynamic rectangle additions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Random Pick with Weight | Medium | Solve | |
| My Calendar II | Medium | Solve | |
| Max Sum of Rectangle No Larger Than K | Hard | Solve | |
| Minimum Absolute Difference Between Elements With Constraint | Medium | Solve | |
| Sum of Absolute Differences in a Sorted Array | Medium | Solve |