Approximate Pi from Random Points
Company: Toma
Role: Frontend Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
You are given an array `pts` of points sampled uniformly at random from the unit square `[0, 1] x [0, 1]`. Each point is represented as an array `[x, y]`, where `0 <= x <= 1` and `0 <= y <= 1`.
A point lies inside the quarter circle of radius `1` if:
`x^2 + y^2 <= 1`
Implement the following function to return an approximation of the value of `pi`:
```javascript
function approx(pts) {
return 0;
}
```
Use the fraction of points that fall inside the quarter circle to estimate `pi`.
Constraints:
- `pts` is never `null`.
- `pts` contains at least one point.
- Every point contains exactly two numeric values.
Quick Answer: This question evaluates a candidate's ability to implement array-processing logic and apply probabilistic numerical methods, combining basic geometric reasoning with Monte Carlo estimation.
Estimate pi as 4 times the fraction of sampled points inside the quarter unit circle.
Constraints
- pts is non-empty
Examples
Input: ([[0, 0], [1, 0], [1, 1], [0.5, 0.5]],)
Expected Output: 3.0
Explanation: Three of four inside.
Input: ([[0.2, 0.2]],)
Expected Output: 4.0
Explanation: Single inside point.