Power Sellers and Vehicle Share of New US Listing Engagement
SQL & DataModel answerLast updated
Two SQL tasks on a marketplace: count sellers with many well-engaged products in a short window, and compute the Vehicle share of interactions on newly created US listings.
Marketplace engagement queries
You work with two tables from a classifieds marketplace.
listing_interaction: one row per buyer, seller, product and day.
| column | meaning |
|---|---|
| buyer_id | the buyer involved |
| seller_id | the seller who owns the listing |
| date | day of the interaction |
| product_id | the listing interacted with |
| li | number of listing interactions that day |
dim_all_product: a daily snapshot of product attributes, so a product can appear once per date.
| column | meaning |
|---|---|
| product_id | the listing |
| category | e.g. Vehicle, Home, Housing |
| date | snapshot day |
| create_date | day the listing was created |
| country | e.g. US, CA, UK |
Part A
Over the last 3 calendar days, how many distinct sellers have more than three products where each of those products has li > 1?
Part B
Considering only listings created in the US within the last 7 days, what percentage of total li comes from the Vehicle category?
What to deliver
- A SQL query (or equivalent pandas code) for each part.
- The assumptions you made about ambiguous wording, such as whether "last N days" includes today, and at what level
li > 1is evaluated. - The edge cases you considered and how you would check that the results are correct.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two tasks look like plain aggregations, but the correctness depends on three things: the grain of each table, the snapshot nature of the product dimension, and the date convention. I would state these assumptions before writing any SQL.
Assumptions
- "Last N days" means the last N complete days,
[CURRENT_DATE - N, CURRENT_DATE). Today is excluded because its partition is usually incomplete. If the interviewer wants today included, only the upper bound changes. - Part A: a product qualifies for a seller if its total
liin the window, summed over buyers and days, is greater than 1. "More than three products" means at least 4, so> 3. - Part B: "listings created in the last 7 days" filters on
create_date. We count interactions on those listings from their creation date up to yesterday. Attributes come from the latest snapshot.
Part A: sellers with more than three well-engaged products
Aggregate in two stages. The first stage works at (seller, product) grain, where the li > 1 test belongs. The second works at seller grain and counts qualifying products.
WITH product_li AS (
SELECT seller_id,
product_id,
SUM(li) AS total_li
FROM listing_interaction
WHERE date >= CURRENT_DATE - INTERVAL '3' DAY
AND date < CURRENT_DATE
GROUP BY seller_id, product_id
HAVING SUM(li) > 1
),
qualifying_sellers AS (
SELECT seller_id
FROM product_li
GROUP BY seller_id
HAVING COUNT(DISTINCT product_id) > 3
)
SELECT COUNT(*) AS num_sellers
FROM qualifying_sellers;
Why it works:
- The
HAVING SUM(li) > 1runs at product grain, so a product is judged on its whole window activity. - Grouping by (seller, product) rather than product alone keeps counts right when a product is linked to more than one seller.
- Each seller appears once in
qualifying_sellers, so the finalCOUNT(*)counts sellers.
Alternative reading: if every interaction row of the product must have li > 1, use HAVING MIN(li) > 1. It is a one-line change, and pointing it out shows you noticed the ambiguity.
Part B: Vehicle share of interactions on new US listings
- Reduce the snapshot table to one row per product (the latest snapshot).
- Keep US listings with
create_datein the last 7 days. - Join to interactions.
- Use conditional aggregation for the ratio.
WITH latest_product AS (
SELECT product_id, category, country, create_date
FROM dim_all_product
WHERE date = (SELECT MAX(date) FROM dim_all_product)
),
new_us AS (
SELECT product_id, category, create_date
FROM latest_product
WHERE country = 'US'
AND create_date >= CURRENT_DATE - INTERVAL '7' DAY
AND create_date < CURRENT_DATE
)
SELECT 100.0 * SUM(CASE WHEN p.category = 'Vehicle' THEN i.li ELSE 0 END)
/ NULLIF(SUM(i.li), 0) AS vehicle_li_pct
FROM new_us p
JOIN listing_interaction i
ON i.product_id = p.product_id
AND i.date >= p.create_date
AND i.date < CURRENT_DATE;
Why it works:
- One row per product on the dimension side means the join cannot fan out and inflate
li. - Numerator and denominator come from the same filtered rows, so the result is a true share between 0 and 100.
100.0 *forces decimal arithmetic; integer division would return 0.NULLIF(..., 0)returns NULL rather than raising an error when there are no interactions.
Snapshot-aware variant: join on i.product_id = d.product_id AND i.date = d.date, which uses the category and country as of the interaction day. It is also safe from fan-out and handles products whose attributes change. Either choice is fine if you explain it. Note that a product's category rarely changes, so the results usually match.
pandas equivalent
import pandas as pd
def sellers_with_many_active_products(inter: pd.DataFrame, today: pd.Timestamp) -> int:
w = inter[(inter["date"] >= today - pd.Timedelta(days=3)) & (inter["date"] < today)]
per_prod = w.groupby(["seller_id", "product_id"], as_index=False)["li"].sum()
per_prod = per_prod[per_prod["li"] > 1]
per_seller = per_prod.groupby("seller_id")["product_id"].nunique()
return int((per_seller > 3).sum())
def vehicle_share_new_us(inter, prod, today):
latest = prod[prod["date"] == prod["date"].max()].drop_duplicates("product_id")
new_us = latest[(latest["country"] == "US")
& (latest["create_date"] >= today - pd.Timedelta(days=7))
& (latest["create_date"] < today)]
j = inter.merge(new_us[["product_id", "category", "create_date"]], on="product_id")
j = j[(j["date_x"] >= j["create_date"]) & (j["date_x"] < today)] if "date_x" in j else \
j[(j["date"] >= j["create_date"]) & (j["date"] < today)]
total = j["li"].sum()
return None if total == 0 else 100.0 * j.loc[j["category"] == "Vehicle", "li"].sum() / total
If both frames carry a date column, rename one before merging to avoid the _x/_y suffixes.
Complexity
With N interaction rows in the window and P products: Part A is one scan and two hash aggregations, O(N) time and space proportional to distinct seller-product pairs. Part B is a hash join of a small filtered set against N rows, O(N + P). On partitioned warehouse tables, putting date predicates directly on the partition column enables pruning, which matters more than the query shape.
Validation and edge cases
- Snapshot fan-out is the most common bug. Check that
SUM(li)after the join equals the sum over the same rows without the join. - Partial today: state your convention and keep it consistent in both parts.
- Off-by-one: "more than three" is
> 3, not>= 3. - NULLs:
SUMignores NULLli; a NULL category lands in the denominator only, which is correct. - Label hygiene:
'vehicle 'versus'Vehicle'may need normalizing withTRIM/LOWERon real data. - Ambiguous
date: both tables have it, so qualify with aliases. - Empty windows: Part A returns 0; Part B returns NULL, not an error.
- Sanity checks: the percentage must be in [0, 100]; hand-compute a few sellers from a small sample and compare; test a seller with exactly 3 and exactly 4 qualifying products.
Common mistakes
- Filtering
li > 1on rows before aggregating, which changes the meaning. - Joining the snapshot table on
product_idonly. - Dividing integers and getting 0.
- Counting rows instead of distinct sellers in the final step.