The Active Businesses interview question revolves around analyzing user engagement data stored in a database. You are typically given an Events table containing business IDs, event types, and an "occurrences" value. An "active business" is defined as a business that has more than one event type where the occurrences for that specific event are greater than the average occurrences for that event type across all businesses.
Companies like Yelp use this Active Businesses coding problem to evaluate a candidate's proficiency with SQL aggregations and subqueries. It tests your ability to calculate a metric (the average) at one level of granularity (event type) and then apply a filter at a different level (business ID). It is a classic example of complex data filtering and business logic implementation.
This problem follows the Database interview pattern of using Common Table Expressions (CTEs) or subqueries with GROUP BY and HAVING. The logic is usually split into two parts:
Imagine the following data:
Average for 'Reviews' is 6. Average for 'Ads' is 3. Business 1 exceeds the average for both (10 > 6 and 5 > 3). Since it has 2 such event types (which is > 1), Business 1 is "Active." Business 2 fails both.
Practice using window functions like AVG(occ) OVER(PARTITION BY event_type). They are often cleaner and more performant than joining a separate aggregation subquery in modern SQL environments.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Active Users | Medium | Solve | |
| Activity Participants | Medium | Solve | |
| All People Report to the Given Manager | Medium | Solve | |
| All the Pairs With the Maximum Number of Common Followers | Medium | Solve | |
| Apples & Oranges | Medium | Solve |