Estimate Pi from Sampled Points
Updated
Problem statement
A Monte Carlo estimate of pi can compare points inside a quarter circle with points inside its enclosing square.
For this exercise, assume the sampled points are already supplied as integer grid coordinates from 0 through 1000. Interpret (x, y) as the unit-square point (x / 1000, y / 1000). Count a point as inside the quarter circle when x² + y² <= 1000000, including its boundary.
Return 4 × insideCount / points.length as a floating-point value. Do not generate new random points. The judge checks this formula; it does not require a small sample or a finite grid to approximate pi closely.
Function
estimatePi(points: int[][]) → doubleExamples
Example 1
points = [[0,0],[1000,1000],[500,500],[1000,0]]return = 3.0Three of the four points are inside or on the quarter circle. The estimate is 4 × 3 / 4 = 3.
Example 2
points = [[1000,1000]]return = 0.0The only point has squared distance 2000000, so no point is counted and the estimate is 0.
Example 3
points = [[600,800],[800,600],[0,1000]]return = 4.0Every point has squared distance 1000000 and lies on the boundary. All three count, so the estimate is 4.
Constraints
1 <= points.length <= 10000- Every point has exactly 2 coordinates.
0 <= every coordinate <= 1000
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- 100 DoorsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Clock Hand Angle with Subsecond PrecisionScale AI · ONSITE INTERVIEWScale AI interview questions
- Recover the Initial Mango PileLSEG · ONSITE INTERVIEWLSEG interview questions
- Add Two Numeric Strings with CarryZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Three- and Five-liter Jug PlanXilinx · ONSITE INTERVIEWXilinx interview questions
- Add Two Numbers Given as Digit ArraysCapital One · ONSITE INTERVIEWCapital One interview questions
- Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions