Pearson Correlation Coefficient
Updated
Problem statement
Given two equal-length arrays x and y of paired observations, calculate their Pearson correlation coefficient without a statistics library.
Let mx and my be their arithmetic means. Return sum((x[i] - mx) * (y[i] - my)) / sqrt(sum((x[i] - mx)^2) * sum((y[i] - my)^2)). Each sum includes every index.
Both arrays have positive variance, so the denominator is nonzero. Keep the sign of the result; do not return covariance or its absolute value. Your answer is accepted when it is within 1e-6 of the right answer, in absolute or relative terms.
The arrays are given, so each run is the same. In a Python study session, you can also generate paired random arrays and check your calculation with numpy.corrcoef(x, y)[0, 1]; your solution does not need NumPy.
Function
correlation(x: double[], y: double[]) → doubleExamples
Example 1
x = [1,2,3]y = [2,4,6]return = 1.0A positive linear relationship has correlation 1.
Example 2
x = [1,2,3]y = [6,4,2]return = -1.0A negative linear relationship has correlation -1.
Example 3
x = [-1,0,1]y = [1,0,1]return = 0.0Centered cross-products cancel, while both variances are positive.
Constraints
2 <= x.length == y.length <= 100000.- Each observation is a finite integer represented as a double, with absolute value at most
1000000. - Each array contains at least two different values.
- Use double-precision arithmetic. NaN, infinities and constant arrays are outside the input contract.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
- Fixed-Size Memory PoolPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Logistic Regression with Gradient DescentYahoo · PHONE SCREENYahoo interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Reachable Transfer PositionsCitadel · PHONE SCREENCitadel interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN