Implement percentage RMSE and bootstrap its CI
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation and statistical reasoning skills, focusing on robust numerical computation of percentage RMSE (including handling zeros/negatives, optional country weights, and numerical stability) and nonparametric bootstrap methods for confidence intervals.
Part 1: Implement percentage RMSE with filtering, weights, and numerical stability
Constraints
- 0 <= len(rows) <= 200000
- -10^15 <= actual_revenue, predicted_revenue <= 10^15
- Weights may be omitted; missing countries default to weight 1.0
- Rows with actual_revenue <= 0, predicted_revenue < 0, or weight <= 0 must be ignored
Examples
Input: ([('US', 100.0, 110.0), ('CA', 200.0, 180.0)], None)
Expected Output: 0.1
Explanation: Both rows have relative error magnitude 0.1, so pRMSE = sqrt((0.01 + 0.01) / 2) = 0.1.
Input: ([('A', 100.0, 120.0), ('B', 100.0, 100.0)], {'A': 3.0, 'B': 1.0})
Expected Output: 0.173205080757
Explanation: Weighted mean square error is (3*0.04 + 1*0.0) / 4 = 0.03, so pRMSE = sqrt(0.03).
Hints
- The expression predicted / actual - 1 is algebraically equal to (predicted - actual) / actual, but the second form is numerically safer when predicted is very close to actual.
- Filter invalid rows first, then compute a weighted mean of squared relative errors.
Part 2: Bootstrap a 95% CI for pRMSE with deterministic resampling
Constraints
- 0 <= len(rows) <= 5000
- 1 <= B <= 5000
- 0 <= seed <= 10^9
- Use n-of-n resampling, where n is the count of valid rows after filtering
- Rows with actual_revenue <= 0, predicted_revenue < 0, or weight <= 0 must be ignored before bootstrapping
Examples
Input: ([('A', 100.0, 100.0), ('B', 100.0, 200.0)], 4, 2, None)
Expected Output: (0.707107, 0.0, 1.0)
Explanation: The original pRMSE is sqrt((0^2 + 1^2)/2) = 0.707107. With the specified generator and B = 4, the bootstrap estimates are [1.0, 0.0, 1.0, 0.707107].
Input: ([('A', 100.0, 110.0), ('B', 200.0, 220.0)], 6, 5, None)
Expected Output: (0.1, 0.1, 0.1)
Explanation: Every valid row has the same relative error 0.1, so every bootstrap sample also has pRMSE 0.1.
Hints
- Resample from the filtered valid rows, not from the raw input.
- Store all bootstrap estimates, sort them, then pick the two required order-statistic positions.
Part 3: Exact probability that a bootstrap resample matches the original sample in order
Constraints
- 0 <= n <= 100
- For n > 0, the exact probability is 1 / n^n
- The fraction should be reduced
Examples
Input: (3,)
Expected Output: (1, 27)
Explanation: There are 3^3 = 27 ordered resamples of length 3, and exactly one matches the original sample in order.
Input: (1,)
Expected Output: (1, 1)
Explanation: With one element, the only possible bootstrap draw matches the original.
Hints
- Each of the n positions in the resample must pick one specific original item.
- There is exactly one successful ordered resample out of n^n equally likely ordered resamples when n > 0.
Part 4: Detect bootstrap risks for pRMSE and recommend mitigations
Constraints
- 0 <= len(rows) <= 200000
- Use only valid rows with actual_revenue > 0 and predicted_revenue >= 0
- Recommendation labels must be unique and sorted lexicographically
- Median is the middle value after sorting, or the average of the two middle values for an even number of items
Examples
Input: ([('US', 'NA', 100.0, 100.0), ('CA', 'NA', 100.0, 90.0), ('MX', 'NA', 100.0, 1000.0), ('FR', 'EU', 100.0, 100.0)],)
Expected Output: ['cluster_bootstrap_by_region', 'collect_more_data', 'report_robust_metric', 'stratified_bootstrap_by_region', 'winsorize_or_log_transform']
Explanation: There are only 4 valid rows, one extreme error dominates the median, and region NA is both repeated across countries and overly dominant.
Input: ([('A', 'R1', 100.0, 102.0), ('B', 'R2', 100.0, 98.0), ('C', 'R3', 100.0, 101.0), ('D', 'R4', 100.0, 99.0), ('E', 'R5', 100.0, 100.0)],)
Expected Output: []
Explanation: There are enough valid rows, no heavy-tail warning, no repeated region with multiple countries, and no region imbalance.
Hints
- Start by filtering valid rows; every later rule should use the filtered data only.
- Heavy tails can be detected by comparing the largest squared relative error against the median, while dependence and imbalance come from region counts and distinct country counts.