KatInterview

Seller Enforcement Timeline and Repeat-Buyer Product Filters

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsconditional-aggregation

Last updated

Three SQL/pandas tasks: find sellers penalized after a reinstatement, get the first event after the latest reinstatement, and find customers who bought A in 2010 and B in 2018.

Context

A marketplace's trust and safety team keeps a log of enforcement actions on sellers. A separate analytics team studies what customers buy over time. You are given two tables.

seller_status: one row per status change for a seller.

seller_iddatestatus
12019-01-01reinstate
12020-06-01block
22020-01-01reinstate
22021-03-01suspend

purchase_history: one row per item ordered.

idorder_dateproduct
102010-05-02A
102018-07-19B
112010-03-11A

Here id identifies the customer. Statuses include reinstate, block and suspend.

Tasks

Write SQL (or pandas) for each of the following:

  1. Return every seller_id that has at least one block or suspend event dated after at least one reinstate event.
  2. For every seller, return the first status recorded after that seller's most recent reinstate.
  3. Return every customer id that bought product 'A' at least once during 2010 and product 'B' at least once during 2018.

What to cover

  • State your assumptions, such as same-day events, sellers with no reinstate, and sellers with nothing after their latest reinstate.
  • Give the expected output on the sample data.
  • Briefly discuss complexity and how you would handle messy data.

Hints

Hint 1

For task 1, you don't need a self-join. Ask what single comparison between one reinstate date and one block/suspend date proves that a valid ordered pair exists.

Hint 2

For task 2, first attach each seller's latest reinstate date to all of their rows (a window MAX over a CASE expression). Then keep only later rows and number them by date.

Hint 3

For task 3, group by customer and use conditional counts in HAVING, one per product/year condition. Prefer half-open date ranges over extracting the year.