Approximate Pi from Random Points
Company: Toma
Role: Frontend Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of Monte Carlo estimation, basic Euclidean geometry, probability, and numerical reasoning when using random sampling to approximate mathematical constants.
Constraints
- `1 <= len(pts) <= 100000`
- Each point has exactly two numeric coordinates
- `0 <= x <= 1` and `0 <= y <= 1` for every point
Examples
Input: [[1, 0]]
Expected Output: 4.0
Explanation: The single point lies exactly on the circle boundary, so it counts as inside. Approximation = 4 * 1 / 1 = 4.0.
Input: [[1, 0], [0, 1], [1, 1], [0.5, 0.5]]
Expected Output: 3.0
Explanation: Three points are inside or on the boundary: [1,0], [0,1], and [0.5,0.5]. One point, [1,1], is outside. Approximation = 4 * 3 / 4 = 3.0.
Input: [[1, 1], [0.9, 0.9]]
Expected Output: 0.0
Explanation: Both points are outside the quarter circle, so the approximation is 4 * 0 / 2 = 0.0.
Input: [[0, 0], [0.6, 0.8], [0.8, 0.6], [0.9, 0.9], [1, 0]]
Expected Output: 3.2
Explanation: Four of the five points are inside or on the boundary: [0,0], [0.6,0.8], [0.8,0.6], and [1,0]. Approximation = 4 * 4 / 5 = 3.2.