Practice Free
HardWindow Functions High Frequency

Cohort Cumulative Expansion Revenue Trajectory

Frequently Asked By:MicrosoftGoogleStripe
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

For each row in `cohort_expansions`, compute the running cumulative expansion revenue for that cohort. Return: 1. `cohort_name` 2. `activity_month` 3. `expansion_mrr` 4. `cumulative_expansion_mrr`: `SUM(expansion_mrr) OVER (PARTITION BY cohort_name ORDER BY activity_month ASC)` Order results by `cohort_name` ascending, then by `activity_month` ascending.

Why Technical Interviewers Ask This

SaaS venture investors track net dollar retention. For each customer signup cohort, finance wants to observe cumulative expansion revenue accumulated over consecutive operating months.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass window functions constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        cohort_name,
        activity_month,
        expansion_mrr,
        SUM(expansion_mrr) OVER (PARTITION BY cohort_name ORDER BY activity_month ASC) AS cumulative_expansion_mrr
      FROM cohort_expansions
      ORDER BY cohort_name ASC, activity_month ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Window Functions. It meets the target criteria: Monthly expansion MRR alongside cumulative cohort expansion total.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Microsoft and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Window Functions queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions