MoM vs YoY Growth Divergence Report
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `growth_divergence_report(df)` in pandas. The input has a `month` column (`datetime64[ns]`, month-end, consecutive, sorted, at least 24 months) and a numeric `revenue` column. Compute month-over-month growth (MoM, lag 1) and year-over-year growth (YoY, lag 12) as fractions, and return only the months where the two figures disagree in sign. These are the months where a MoM-only read contradicts the yearly trend.
The output must follow these rules:
- Round `mom_pct` and `yoy_pct` to 4 decimal places before the sign test, and output the rounded values.
- Columns, in this order: `month` (`datetime64[ns]`), `revenue`, `mom_pct`, `yoy_pct` (all three `float64`), and `note` (`object`).
- `note` is `'mom_up_yoy_down'` when `mom_pct > 0` and `yoy_pct < 0`, and `'mom_down_yoy_up'` when `mom_pct < 0` and `yoy_pct > 0`.
- Exclude months where either figure is `NaN` or exactly `0`, and months where the signs agree.
- Sort by `month` ascending and finish with `reset_index(drop=True)`. If no month diverges, return an empty DataFrame with the same five columns and dtypes.
For example, if revenue was 150 in March 2023, 100 in February 2024 and 120 in March 2024, then March 2024 has `mom_pct = 0.2` and `yoy_pct = -0.2`, so it appears in the output with note `'mom_up_yoy_down'`.
```hint Warm-up rows
Before writing explicit `NaN` handling for the first 12 months, check what `NaN > 0` and `NaN < 0` evaluate to.
```
```hint Tiny growth
Work out what should happen to a month whose MoM growth is 0.00003, given the rounding rule.
```
### Constraints & Assumptions
- At least 24 rows, one per month, consecutive, month-end and sorted ascending. No gap handling is required.
- Because there are no gaps, the row 12 positions earlier is the same calendar month one year before.
- `revenue` is numeric and may be stored as integers.
### What a Strong Answer Covers
- How the lag-1 and lag-12 growth rates are computed and what the first 12 rows contain
- Where the rounding happens relative to the sign test, and its effect on very small growth values
- A filter that keeps only strict sign disagreements and assigns the matching `note`
- Column order, dtypes, sorting and index, including the empty result
- Inputs the spec leaves open, such as zero revenue in the base month
### Follow-up Questions
- How would you handle a zero or missing `revenue` value in the divisor?
- How would you extend the report to show the size of the divergence, beyond its sign?
- How would the function change if the input could skip months?
Overview: Write a pandas function that compares month-over-month and year-over-year revenue growth and returns only the months where the two disagree in sign. It tests lagged growth calculations, rounding before a sign test, NaN and zero handling, and keeping exact dtypes when the result is empty.