The Customers Who Bought All Products interview question is a relational division problem. Given a Customer table (tracking which customers bought which product IDs) and a Product table (listing all available product IDs), you need to find the customers who have purchased every single product listed in the Product table. This Customers Who Bought All Products coding problem is about comparing a subset's size to the global set's size.
Companies like Apple and Bloomberg use this to test a candidate's ability to use subqueries and dynamic counting. Instead of hardcoding the number of products, your query must work regardless of how many products are in the database. It tests the concept of "Double Negation" (finding customers for whom there does not exist a product they haven't bought) or simple cardinality comparison.
This utilizes the Database interview pattern of "Compare Aggregate to Subquery."
Products: {1, 2}. Customer A buys: {1, 2}. (Count 2) Customer B buys: {1, 1, 1}. (Count 1) Customer C buys: {2}. (Count 1) Only Customer A matches the total count of 2.
Relational division is a classic SQL concept. Practice the "count comparison" method as it is usually the most performant and easiest to explain. For very advanced roles, be prepared to discuss the NOT EXISTS double-nested subquery method as well.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Confirmation Rate | Medium | Solve | |
| Product Sales Analysis III | Medium | Solve | |
| Rank Scores | Medium | Solve | |
| Product Price at a Given Date | Medium | Solve | |
| Friend Requests II: Who Has the Most Friends | Medium | Solve |