Implement trapezoidal rule integration in NumPy for non-uniformly spaced samples

Quick Overview

Implement composite trapezoidal integration in NumPy for samples taken at unevenly spaced points, without calling a library integration routine. Tests vectorized array slicing, handling of non-uniform step sizes, and reasoning about numerical integration error.

Implement trapezoidal rule integration in NumPy for non-uniformly spaced samples

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `integrate(y: np.ndarray, x: np.ndarray) -> float` that computes the definite integral of samples `y` at points `x` using the composite trapezoidal rule. Spacing in `x` may be non-uniform, so do not assume a constant step. Requirements: - The area of interval `i` is `0.5 * (x[i+1] - x[i]) * (y[i] + y[i+1])`. - Return a Python `float`. - Prefer a vectorized implementation over an explicit loop. - Do not call `np.trapz` (or `np.trapezoid`, its name in NumPy 2). Example: ```python x = np.array([0.0, 1.0, 3.0]) y = np.array([0.0, 1.0, 0.0]) integrate(y, x) # -> 1.5 ``` The first interval has width 1 and contributes `0.5 * 1 * (0 + 1) = 0.5`. The second has width 2 and contributes `0.5 * 2 * (1 + 0) = 1.0`, so the total is 1.5. ```hint Think in arrays Every interval needs its own width and its two endpoint heights. Build all the widths and all the endpoint pairs as arrays at once, then combine them element-wise. ``` ### Constraints & Assumptions - `len(x) == len(y)`, and both have at least 2 elements. - `x` is strictly increasing. ### What a Strong Answer Covers - Takes each interval's width from consecutive `x` values, so uneven spacing still gives the right area. - Builds the widths and endpoint pairs with vectorized operations, with no Python loop and no library trapezoid routine. - Returns a plain Python `float` and reproduces the example value. - States the time complexity and the extra memory used by intermediate arrays. - Handles input pitfalls such as integer dtypes, mismatched lengths and the two-point case. ### Follow-up Questions - How would you extend this to Simpson's rule, and what changes when the number of intervals is odd? - How does the trapezoidal error scale with the number of points, and for which functions is the rule exact? - How would you integrate a 2D array along one chosen axis?

Overview: Implement composite trapezoidal integration in NumPy for samples taken at unevenly spaced points, without calling a library integration routine. Tests vectorized array slicing, handling of non-uniform step sizes, and reasoning about numerical integration error.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement integrate(y: np.ndarray, x: np.ndarray) -> float that computes the definite integral of samples y at points x using the composite trapezoidal rule. Spacing in x may be non-uniform, so do not assume a constant step.

Requirements:

  • The area of interval i is 0.5 * (x[i+1] - x[i]) * (y[i] + y[i+1]) .
  • Return a Python float .
  • Prefer a vectorized implementation over an explicit loop.
  • Do not call np.trapz (or np.trapezoid , its name in NumPy 2).

Example:

x = np.array([0.0, 1.0, 3.0])
y = np.array([0.0, 1.0, 0.0])
integrate(y, x)  # -> 1.5

The first interval has width 1 and contributes 0.5 * 1 * (0 + 1) = 0.5. The second has width 2 and contributes 0.5 * 2 * (1 + 0) = 1.0, so the total is 1.5.

Constraints & Assumptions

  • len(x) == len(y) , and both have at least 2 elements.
  • x is strictly increasing.

What a Strong Answer Covers Guidance

  • Takes each interval's width from consecutive x values, so uneven spacing still gives the right area.
  • Builds the widths and endpoint pairs with vectorized operations, with no Python loop and no library trapezoid routine.
  • Returns a plain Python float and reproduces the example value.
  • States the time complexity and the extra memory used by intermediate arrays.
  • Handles input pitfalls such as integer dtypes, mismatched lengths and the two-point case.

Follow-up Questions Guidance

  • How would you extend this to Simpson's rule, and what changes when the number of intervals is odd?
  • How does the trapezoidal error scale with the number of points, and for which functions is the rule exact?
  • How would you integrate a 2D array along one chosen axis?