Correlation between metrics

Correlation between metrics

Meta Statistics Interview Question

Meta checks whether two product metrics move together before anyone claims one drives the other.

Write correlation(xs, ys) that returns the Pearson correlation of two equal-length lists, rounded to four decimal places. Return 0.0 when either list is completely flat, since a flat list moves with nothing.

Asked of

  • Product Analyst
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

xs = [1, 2, 3], ys = [2, 4, 6]

Output

1

Example 2

Input

xs = [1, 2, 3], ys = [6, 4, 2]

Output

-1

Example 3

Input

xs = [1, 2, 3], ys = [5, 5, 5]

Output

0

Explanation

For [1, 2, 3] against [2, 4, 6] every step up in the first is matched by a step up in the second, so the correlation is exactly 1. Against [6, 4, 2] the steps are matched in the opposite direction, so it is -1.

Submit also runs 2 hidden test cases that check edge cases.

Similar questions