Count Distinct Users by Department and Time Window
SQL & DataAuto-graded · 18 test casesLast updated
Tests set-based deduplication and join logic to find unique users who interacted with products in a specific department during a time range.
You are given two datasets representing product catalog information and user interaction logs.
Products dataset: Each entry contains [product_id, department, category, subcategory] where the department is the top-level classification.
Clicks dataset: Each entry contains [user_id, product_id, timestamp] representing a user clicking on a product at a specific time.
Write a function that calculates how many distinct users clicked on at least one product belonging to a specified department within a given time window.
Requirements:
- The time window is defined by
start_ts(inclusive) andend_ts(exclusive): count clicks wherestart_ts <= timestamp < end_ts - A user who clicks multiple products within the same department should be counted exactly once
- Clicks on products not present in the products dataset should be ignored
- The department match must be exact (case-sensitive)
Constraints:
- 0 ≤ len(products) ≤ 10⁵
- 0 ≤ len(clicks) ≤ 10⁶
- All product_ids, user_ids, and timestamps are non-negative integers
- Department, category, and subcategory are non-empty strings
- start_ts ≤ end_ts
Examples
Example 1
Input: count_unique_users_by_department([[101, 'Electronics', 'Phones', 'Smartphones'], [102, 'Electronics', 'Computers', 'Laptops'], [201, 'Home', 'Kitchen', 'Appliances']], [[1, 101, 10], [1, 102, 20], [2, 101, 30], [3, 201, 40], [2, 101, 50]], 'Electronics', 0, 40)
Output: 2
Example 2
Input: count_unique_users_by_department([[1, 'A', 'C1', 'S1'], [2, 'A', 'C2', 'S2'], [3, 'B', 'C3', 'S3']], [[10, 1, 100], [10, 2, 101], [11, 3, 101], [12, 1, 200]], 'A', 100, 200)
Output: 1
Hints
Hint 1
Build a lookup structure mapping product IDs to their departments before processing clicks
Hint 2
Use a set data structure to automatically handle user deduplication
Hint 3
Remember that the time window is half-open: include the start boundary but exclude the end boundary
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution requires two key operations: mapping products to departments and deduplicating users.
First, create a hash map from the products dataset where each product ID points to its department. This allows constant-time lookup when processing clicks.
Second, iterate through all clicks and filter based on two conditions:
- The timestamp falls within the half-open interval
[start_ts, end_ts) - The product belongs to the target department (via the lookup map)
For qualifying clicks, insert the user ID into a set. Sets automatically handle deduplication, so users who click multiple products in the same department are counted only once. Clicks referencing unknown product IDs return None from the lookup and fail the equality check, correctly excluding them.
The half-open interval semantics ensure that consecutive time windows never double-count boundary events.
Correctness
Every user counted has at least one valid click: a click within the time window on a product in the target department. Conversely, every user with such a click gets added to the set at least once. The set guarantees each qualifying user contributes exactly 1 to the final count, regardless of how many times they appear in the filtered clicks.
Complexity
Time: O(P + C) where P is the number of products and C is the number of clicks. Building the lookup map requires one pass over products, and filtering clicks requires one pass with O(1) average-case hash operations.
Space: O(P + U) where U is the number of distinct qualifying users. The product map stores all products, and the set stores unique user IDs that match the criteria.
Implementation
from typing import List
def count_unique_users_by_department(products: List[List], clicks: List[List], department: str, start_ts: int, end_ts: int) -> int:
product_to_dept = {
row[0]: row[1]
for row in products
}
unique_users = set()
for user_id, product_id, timestamp in clicks:
if start_ts <= timestamp < end_ts and product_to_dept.get(product_id) == department:
unique_users.add(user_id)
return len(unique_users)