Estimate Pi with a Finite Series
Updated
Problem statement
Write a function that estimates pi using a finite series.
For this exercise, assume the required estimate is 4 * sum((-1)^k / (2*k + 1)) for integer k from 0 through terms - 1. Return this finite sum, rather than the built-in value of pi.
Each additional term alternates sign. The result approaches pi as the number of terms grows.
Function
estimatePi(terms: int) → doubleExamples
Example 1
terms = 1return = 4.0The first term is 4 / 1 = 4.
Example 2
terms = 2return = 2.6666666666666665The first two terms give 4 * (1 - 1/3) = 8/3.
Example 3
terms = 3return = 3.466666666666667The next positive term gives 4 * (1 - 1/3 + 1/5) = 52/15.
Constraints
1 <= terms <= 1000000.- Return a finite decimal value. The answer is accepted with an absolute-or-relative tolerance of
10^-9against the stated finite sum.
More Squarepoint Capital interview questions
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Maximum SubarrayONSITE INTERVIEW · Seen Aug 2026
- Product of Array Except SelfONSITE INTERVIEW · Seen Aug 2026
- In-Memory Directory and File OperationsONSITE INTERVIEW · Seen Oct 2025
- Count C++ Constructor and Assignment CallsONSITE INTERVIEW · Seen Jun 2025
- Observe a Lazy GeneratorONSITE INTERVIEW · Seen Feb 2025
- Order Matching on ExecuteONSITE INTERVIEW · Seen Dec 2024
- Accumulate Numeric Value ObjectsONSITE INTERVIEW · Seen Sep 2024
- Differentiate a PolynomialONSITE INTERVIEW · Seen Aug 2023
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Climbing StairsAmazon · ONSITE INTERVIEW
- Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions