← Questions

Unique visitors per department from click logs

0
AmazonCodingLast asked Sep 2025

A coding interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests hashing, trees and data processing.

An online store has two tables. products(product_id, department, category, subcategory) places every product in a hierarchy: a department contains categories, which contain subcategories. clicks(user_id, product_id, event_ts) records every click on a product. Write a SQL query that returns the number of distinct customers who clicked any product in a given department within a given time range. A customer who clicked many products in the department counts once.

Interface

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

  • Parameters :department, :start_ts and :end_ts, with the range [:start_ts, :end_ts).
  • The query returns one row with one column, unique_visitors.

Example

products                                    clicks
product_id  department   category  subcat   user_id  product_id  event_ts
p1          Electronics  Phones    Cases    u1       p1          2026-03-01 10:00
p2          Electronics  Laptops   Bags     u1       p2          2026-03-01 11:00
p3          Books        Fiction   Mystery  u2       p1          2026-03-02 09:00
                                            u3       p3          2026-03-02 09:30
                                            u4       p2          2026-04-05 08:00

department = 'Electronics', range [2026-03-01, 2026-04-01)  ->  unique_visitors = 2
# u1 (twice) and u2; u3 clicked Books; u4's click is outside the range

Constraints

  • Billions of clicks; millions of products.
  • Every product has exactly one department.

Clarifying questions

  • Is the range inclusive at both ends?
  • Do clicks on products missing from products count?
  • Should bots or internal users be excluded?
  • Is the answer needed for one department at a time, or for all of them at once?

What a strong answer covers

  • A join from clicks to products on product_id, filters on department and time, and COUNT(DISTINCT c.user_id); the time filter applied to clicks so partition pruning works.
  • Avoiding double counting: count distinct users, not rows, and never sum per-category counts (a user who clicked two categories would count twice).
  • All departments at once: GROUP BY p.department.
  • Indexing and partitioning: clicks partitioned by date, an index or clustering on (product_id, event_ts) or (event_ts, product_id); products small enough to broadcast in the join.
  • At very large scale, approximate distinct counts (HyperLogLog), whose daily sketches can be merged across ranges, and pre-aggregated daily tables of (day, department, user_id).

Follow-ups

  • Return the count for every department and every category in one query.
  • Return the departments whose unique visitors grew most compared with the previous period.
  • Write the same computation in Python over files too large for memory.

Where it's asked

  • Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

hashing · trees · data processing

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