Integrate a Sampled Function
Updated
Problem statement
For this exercise, assume a function is supplied by paired samples (xs[i], ys[i]). Between consecutive samples, the function follows the straight line joining them.
Return its signed definite integral from xs[0] to the last x coordinate. Areas below the x-axis are negative. Return a floating-point value; the judge accepts an absolute error of at most 10⁻⁶ × max(1, |expected result|).
Function
integrateSamples(xs: int[], ys: int[]) → doubleExamples
Example 1
xs = [0,1,3]ys = [0,2,2]return = 5.0The first interval has area (0 + 2) × 1 / 2 = 1. The second has area (2 + 2) × 2 / 2 = 4. Their sum is 5.
Example 2
xs = [-1,1]ys = [-2,2]return = 0.0The line crosses the origin. Its negative and positive triangular areas cancel, giving signed integral 0.
Example 3
xs = [0,2,5]ys = [-3,-3,-1]return = -12.0The first interval contributes -3 × 2 = -6. The second contributes (-3 - 1) × 3 / 2 = -6. The signed total is -12.
Constraints
2 <= xs.length, ys.length <= 2001-1000 <= xs[i], ys[i] <= 1000- The arrays have equal length.
- The x coordinates are strictly increasing.
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
- 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
- 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
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Add One to a Number Represented as DigitsOracle · ONSITE INTERVIEWOracle interview questions
- Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
- JPMorgan Code for Good India: 10 Experiences (Part 4 of 5)