FastPrepEstimate Pi from Uniform Samples

Estimate Pi from Uniform Samples

Microsoft logoMicrosoft● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given independent points already sampled uniformly from the unit square [0,1] x [0,1]. Each row of samples contains one point [x,y].

Estimate pi as four times the fraction of points on or inside the quarter unit circle: x*x + y*y <= 1.

Function

estimatePi(samples: double[][]) → double

Examples

Example 1

samples = [[0.0,0.0],[1.0,1.0],[0.5,0.5],[0.9,0.1]]return = 3.0

Three of four points lie in the quarter circle, so the estimate is 4 * 3 / 4 = 3.

Example 2

samples = [[1.0,0.0],[0.0,1.0],[1.0,1.0]]return = 2.6666666666666665

Boundary points are included, so two of the three points count.

Constraints

  • 1 <= samples.length <= 100000
  • samples[i].length = 2
  • 0 <= samples[i][j] <= 1
  • Results use absolute tolerance 1e-12

More Microsoft problems

See Microsoft hiring insights
public double estimatePi(double[][] samples) {
  // Write your code here.
}
samples[[0.0,0.0],[1.0,1.0],[0.5,0.5],[0.9,0.1]]
expected3.0
Checking account…