Write four edge-case-safe R utilities: safe ratio, group means, min-max scaling and latest-row dedup
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write four R utility functions that meet the contract below. You may load packages with `library()`. Each function covers an edge case that naive versions get wrong: division by zero, grouping with missing values, constant input to a scaler, and duplicate records.
- `safe_ratio(numerator, denominator)` recycles both inputs to a common length and returns `numerator / denominator`, with `NA_real_` wherever `denominator == 0`. A zero denominator must never produce `Inf` or `NaN`.
- `group_means(df, group, value)` takes column names as strings and returns a data.frame with columns `group` and `mean`: one row per distinct group, missing values removed, sorted ascending by group, and the group column keeping its original type.
- `min_max_normalize(x)` scales `x` into `[0, 1]`, using `na.rm = TRUE` for the min and max and keeping NA positions. If `max == min`, it returns zeros of length `length(x)`.
- `dedup_latest(df)` takes a data.frame with columns `id`, `timestamp` (numeric or Date) and `value`. It keeps the row with the largest `timestamp` for each `id`, sorted ascending by `id`, with default row names.
Examples:
```r
safe_ratio(c(1, 2, 3), c(2, 0, 3))
# [1] 0.5 NA 1.0
min_max_normalize(c(0, 5, 10, NA))
# [1] 0.0 0.5 1.0 NA
```
```hint Where special values come from
For each function, list the inputs that make base R return `Inf`, `NaN`, a warning or a dropped row (a zero denominator, a group whose values are all missing, a constant vector, a missing timestamp), and decide what the contract wants instead.
```
```hint One row per id
Sort so that the row you want sits in a predictable position within each id, then keep exactly one row per id.
```
### Clarifying Questions to Ask
- Should a group whose values are all missing appear with mean `NA`, or be dropped? And what about rows whose group itself is `NA`?
- For a constant vector that also contains `NA`, should the `NA` positions stay `NA` or become 0?
- When two rows for one id share the largest timestamp, which one should be kept, and how should a missing timestamp rank?
- Does "never `Inf` or `NaN`" cover only zero denominators, or every input, including a numerator that is already `Inf`?
### What a Strong Answer Covers
- Masks zero denominators after recycling, so division by zero never leaks `Inf` or `NaN`
- Computes group means without silently losing groups, keeps the group column's type and sorts by it
- Guards the constant and all-missing cases in the scaler while keeping `NA` positions
- Picks the latest row per id deterministically, including ties and missing timestamps, and resets row names
- Traces the examples and states how it resolves the cases the contract leaves open
### Follow-up Questions
- How would you keep `group_means` fast on a large data.frame?
- Why is `denominator == 0` a fragile check for floating-point inputs?
- Which test cases would you write to show that all four functions meet the contract?
Overview: Write four R functions that must behave correctly on edge cases: a ratio that returns NA for zero denominators, per-group means with missing values, min-max scaling of constant or missing input, and keeping the latest row per id. It tests recycling, NA handling, type preservation and deterministic sorting.