Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Numerical Integration Function
00:00
5 left

Numerical Integration Function

MediumPython

Problem

U.S. Bank National Association analyzes sampled risk and pricing curves whose exact integrals may not be available in closed form. Implement composite Simpson's rule to approximate the definite integral of a curve from a to b.

Formal Specification

Write integrate_simpson(values, a, b, n), where values is an array of n + 1 numeric samples. The samples are ordered from left to right and represent f(a + i * h), where h = (b - a) / n. Return the numerical approximation as a floating-point number.

Composite Simpson's rule is:

integral = (h / 3) * (f(a) + f(b) + 4 * sum(odd-indexed samples) + 2 * sum(even-indexed interior samples))

Constraints

  • 2 <= n <= 10^6
  • n is even
  • len(values) == n + 1
  • a < b
  • Sample values are finite numbers in [-10^9, 10^9]

Function Signature

def integrate_simpson(values, a, b, n):
Interviewer

Your question is Numerical Integration Function. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.