Keep the Latest Profile Record per User from a Change Log
Last updated: November 29, 2026
Quick Overview
Given a change-data-capture table of Facebook profile updates where the same user can appear many times (including exact duplicate rows from replays), return one current row per user with their latest city and relationship status.
Meta
November 29, 20260
0
0 solved
Given a change-data-capture table of Facebook profile updates where the same user can appear many times (including exact duplicate rows from replays), return one current row per user with their latest city and relationship status.
This is a warm-up in Meta data engineering screens, where most warehouse tables are fed by append-only logs that get replayed. The interviewer checks whether you dedupe with a window function and a deterministic tie-breaker rather than GROUP BY with MAX on each column.
What the Interviewer Expects
- Uses ROW_NUMBER partitioned by user_id ordered by update time descending
- Adds a tie-breaker so exact-timestamp duplicates resolve deterministically
- Explains why MAX(city) per user is wrong
- Handles delete markers in the log
- Mentions idempotency of the pipeline when replays happen
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you handle a row with op = 'DELETE' as the latest event?
- How would you make this incremental for a daily partitioned table?
- What if two rows share the same timestamp and the same sequence number?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table profile_changes(user_id, city, relationship_status, op, updated_at, log_seq). op is INSERT, UPDATE or DELETE. log_seq is a monotonically increasing offset from the log, which I will use as...
Approach
Rank rows per user from newest to oldest and keep rank 1. Taking MAX(city) and MAX(updated_at) separately per user is a classic bug: it can stitch the city from one row onto the status from anothe...