Implement four DS coding tasks
Company: Roblox
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
Quick Answer: This multi-part question evaluates a data scientist's competencies in statistical inference (sample size and z-test calculations), causal inference and parallel-trends validation (difference-in-differences), Bayesian probability updating, and interpretable supervised learning feature importance, all framed as coding tasks.
Part 1: Two-Sample Z-Test Required Sample Size
Constraints
- 1 <= len(x) <= 10^5
- 0 < alpha < 1
- 0 < power < 1
- effect_size > 0
- Use the population standard deviation: sqrt(sum((xi - mean)^2) / len(x))
Examples
Input: ([10, 12, 14, 16], 0.05, 0.8, 2.0)
Expected Output: 20
Explanation: The population variance is 5, so sigma = sqrt(5). Plugging into the formula gives about 19.62, so the answer is 20.
Input: ([1, 2, 3, 4, 5], 0.1, 0.9, 1.5)
Expected Output: 16
Explanation: The population variance is 2. The computed n is about 15.22, so the minimum integer per-group size is 16.
Hints
- For equal-sized groups in a two-sample z-test, the standard formula is n = 2 * sigma^2 * (z_(1-alpha/2) + z_power)^2 / effect_size^2.
- After computing the real-valued sample size, take the ceiling because you need the minimum integer that still satisfies the requirement.
Part 2: Difference-in-Differences with Pre-Trend Validation
Constraints
- len(period) == len(group) == len(outcome)
- 4 <= len(period) <= 10^5
- group[i] is either 0 or 1
- Each group has at least one observation in the overall pre bucket and the overall post bucket
- For every distinct pre period used in trend validation, both groups appear at least once
Examples
Input: (['pre', 'pre', 'post', 'post'], [0, 1, 0, 1], [10, 12, 11, 15], 0.5)
Expected Output: (2.0, True)
Explanation: Single pre period, so trend validation automatically passes. DiD = (15 - 12) - (11 - 10) = 2.
Input: (['pre1', 'pre1', 'pre2', 'pre2', 'post', 'post'], [0, 1, 0, 1, 0, 1], [10, 12, 11, 13, 12, 17], 0.1)
Expected Output: (3.0, True)
Explanation: Pre-period differences are 2 and 2, so the maximum change is 0 <= 0.1. The DiD estimate is 3.0.
Hints
- First compute overall means for treatment/control in the combined pre and combined post buckets to get the DiD estimate.
- For trend validation, compute treatment minus control separately for each distinct pre period, sort the pre periods by time order, then look at consecutive changes.
Part 3: Bayes' Rule Posterior Probability
Constraints
- 0 <= p_A <= 1
- 0 <= p_B_given_A <= 1
- 0 <= p_B_given_not_A <= 1
- p_B_given_A * p_A + p_B_given_not_A * (1 - p_A) > 0
Examples
Input: (0.01, 0.9, 0.05)
Expected Output: 0.15384615384615385
Explanation: A standard rare-event example: even with strong evidence, the base rate matters.
Input: (0.3, 0.8, 0.2)
Expected Output: 0.631578947368421
Explanation: Numerator = 0.24 and denominator = 0.38.
Hints
- Start from Bayes' rule: P(A|B) = P(B|A)P(A) / P(B).
- Compute P(B) by splitting on whether A happens or not.
Part 4: Logistic Regression Top-3 Features
Constraints
- 3 <= number of features <= 20
- 1 <= number of samples <= 500
- len(feature_names) == number of features
- Each row in X has length equal to len(y)
- y contains only 0 and 1
- y contains at least one 0 and at least one 1
Examples
Input: ([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]], [0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1], ['A', 'B', 'C', 'D'])
Expected Output: ['A', 'B', 'C']
Explanation: The grouped synthetic data is built so feature A has the strongest positive effect, followed by B, then C.
Input: ([[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]], [0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1], ['beta', 'alpha', 'gamma', 'delta'])
Expected Output: ['alpha', 'beta', 'gamma']
Explanation: Alpha and beta are tied in strength, so the tie is broken alphabetically by feature name.
Hints
- Because X is feature-major, you may want to conceptually transpose it so each training example becomes one row.
- A simple way to fit logistic regression without external libraries is Newton's method (IRLS) or gradient descent with an intercept term.