KatInterview

Area-Weighted Random Point Sampling Across Multiple Squares

Data Structure DesignModel answer
MediumModel answerData Structure Designprobabilityprefix-sumssampling

Last updated

Design a class for uniform random point sampling inside a square, then extend it to sample uniformly from the union of N non-overlapping squares.

Area-Weighted Random Point Sampling Across Multiple Squares

Background

Generating uniformly distributed random points over geometric regions comes up in simulation, game development, procedural generation, and statistical testing. Getting it right requires more than picking a random shape and a random point — the selection has to be weighted by area, not by count.

Part 1: Single Square

Design a class Square that represents an axis-aligned square on the 2D plane. The square is defined by its bottom-left corner (x, y) and its side length s.

Implement a method that returns a uniformly random point (px, py) inside the square. Every point in the interior should be equally likely.

Part 2: Union of N Squares

Design a second class, SquareSampler, that takes a list of N non-overlapping Square objects and supports drawing a uniformly random point from their union.

"Uniform over the union" means: the probability that a sample falls in any region of area A is exactly A / totalArea, regardless of which square it belongs to.

Specify:

  • The full API you would expose, with parameter names and return types
  • The data structures used internally and why
  • The step-by-step sampling algorithm
  • Time and space complexity for both construction and each sample call
  • How you would handle floating-point precision issues that could introduce bias

Constraints and rules

  • Side lengths are non-negative. Squares with side length zero may appear in the input and should be silently ignored.
  • An empty input list, or one where every square has zero area, is invalid — raise an error.
  • Negative coordinates for (x, y) are allowed; only the side length must be non-negative.
  • The squares are guaranteed non-overlapping. You do not need to verify this, but explain how you would handle it if you did.
  • The RNG should be injectable for testability.

What the interviewer is looking for

  • Correctly weighting square selection by area rather than by count (the most common bug)
  • A clean two-stage sampling algorithm with justified correctness
  • Awareness of floating-point edge cases that can cause out-of-bounds indexing or vanishing small squares
  • A working implementation with clear complexity analysis

Hints

Hint 1

A uniform point inside a single axis-aligned square is just two independent uniform draws, one per axis. Think about why the product structure of the region makes this exact.

Hint 2

When picking which square to sample from, choosing each square with equal probability 1/N is wrong unless all squares are the same size. What probability should square i have, and how do you efficiently draw from that distribution?

Hint 3

After building prefix sums over areas and drawing a uniform random number r in [0, totalArea), you need to find which square owns r. What can r * totalArea round to, and how does that break your index lookup?