← Questions

Cumulative user metrics with a full outer join

0
MetaCodingLast asked Aug 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Aug 2025. It tests sql, data engineering and incremental loads.

A daily job maintains a cumulative table: for each content item, the running total of a metric up to a given date. Today's run combines yesterday's cumulative snapshot with today's daily values. Write the SQL for one date D.

Interface

Unless you agree on a different schema with the interviewer, assume:

  • daily_metrics(date DATE, content_id VARCHAR(50), daily_value BIGINT): one row per item per day it had activity.
  • cumulative_metrics(date DATE, content_id VARCHAR(50), cumulative_value BIGINT): the cumulative total per item as of a date. Before today's run it holds rows up to D - 1 only.
  • Output, for D = '2025-06-01': date (always D), content_id, cumulative_value = COALESCE(yesterday, 0) + COALESCE(today, 0), one row for every content_id in yesterday's snapshot or in today's activity.

Example

daily_metrics (date = 2025-06-01)        cumulative_metrics (date = 2025-05-31)
content_id | daily_value                 content_id | cumulative_value
a          | 5                           a          | 100
c          | 7                           b          | 40

result
date       | content_id | cumulative_value
2025-06-01 | a          | 105      # in both
2025-06-01 | b          | 40       # no activity today keeps its total
2025-06-01 | c          | 7        # new today

Constraints

  • Filter each table to its own date before the join (d.date = D, y.date = D - 1).
  • content_id is unique within one date in each table.

Clarifying questions

  • Can an item appear more than once in daily_metrics for one day, so I must sum it first?
  • Should items that no longer exist ever drop out of the cumulative table?
  • Is cumulative_metrics partitioned by date, and does the job write one partition per run?
  • What should happen if yesterday's partition is missing?

What a strong answer covers

  • Filter in CTEs or subqueries first; putting the date filter in the WHERE clause after a full outer join silently turns it into an inner or one-sided join.
  • FULL OUTER JOIN ... ON d.content_id = y.content_id, with COALESCE(d.content_id, y.content_id) for the key and COALESCE(..., 0) on both values.
  • Safe reruns: write the result as an overwrite of partition D (insert-overwrite or delete-then-insert in one transaction), never an append, so running the job twice gives the same table.
  • Dependencies: the run for D needs partition D - 1, so backfills run in date order.
  • Tests: an item only in yesterday, only in today, in both, and a rerun of the same date.

Follow-ups

  • Also keep a 7-day rolling total and the last active date per item.
  • A late event for D - 3 arrives today. How do you correct the cumulative table?
  • Compare this cumulative design with recomputing the total from the full daily history each run.

Where it's asked

  • Meta · AI-enabled coding · Last asked Aug 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

sql · data engineering · incremental loads

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding