Integral of a Sine Power
Updated
Problem statement
Return the definite integral of sin(x)ⁿ from 0 to π.
For this exercise, assume n is a nonnegative integer. The integral for n = 0 is π. Return a floating-point result; the judge accepts an absolute error of at most 10⁻⁶ × max(1, |expected result|).
Function
sinPowerIntegral(n: int) → doubleExamples
Example 1
n = 0return = 3.141592653589793The integrand is 1, so its integral over an interval of length π is π.
Example 2
n = 1return = 2.0An antiderivative of sin(x) is -cos(x). Evaluating it at π and 0 gives 1 - (-1) = 2.
Example 3
n = 2return = 1.5707963267948966Using sin²(x) = (1 - cos(2x)) / 2, the cosine term integrates to 0 and the result is π / 2.
Constraints
0 <= n <= 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
- Climbing StairsAmazon · ONSITE INTERVIEW
- Climbing StairsAMD · ONSITE INTERVIEWAMD interview questions
- Climb Stairs with One, Two, or Three StepsAgoda · ONSITE INTERVIEWAgoda interview questions
- Climbing StairsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Climbing StairsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- The Nth Number in the Fibonacci SequenceCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions