Longest Reign at Number One Among Marketplace Categories per Country

Last updated: November 29, 2026

Quick Overview

For each country, find the Marketplace category that held the top spot by daily GMV for the longest unbroken run of days, returning the category, run start, run end and length.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


For each country, find the Marketplace category that held the top spot by daily GMV for the longest unbroken run of days, returning the category, run start, run end and length.

This combines a top-1-per-day ranking with gaps-and-islands, two of the most common window patterns in Meta's SQL loops. The interviewer checks a deterministic daily winner, the row-number-difference trick on the winners sequence, and a tie-break for equal-length reigns.

What the Interviewer Expects
  • Aggregates GMV to country, day and category
  • Picks one winner per country-day with ROW_NUMBER and a tie-break
  • Assigns islands with the difference of two ROW_NUMBERs
  • Computes run length and boundaries per island
  • Selects the longest run per country with a deterministic tie-break
Key Topics to Cover
ROW_NUMBER
gaps and islands
row number difference
top-1 per group
Marketplace GMV
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • What changes if a day has no transactions in a country at all?
  • How would you report every category's longest reign, not only the overall longest?
  • How would you smooth noisy daily winners, for example a 7-day rolling GMV ranking?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Table: marketplace_orders(order_id, country, category, ordered_at, gmv_usd). I assume every country has transactions every day in the period. If not, the island trick still treats missing days as 'n...

Approach
  1. Daily GMV per (country, ds, category).
  2. ROW_NUMBER() per (country, ds) by GMV desc, category asc; keep 1. Now each country has exactly one row per day.
  3. Islands: `ROW_NUMBER() OVER (PARTI...

Submit Your Answer
Markdown supported