The Active Users interview question focuses on identifying "loyal" users from a login history table. Specifically, you need to find users who have logged into the system for five or more consecutive days. This is a common requirement for generating retention reports or engagement metrics.
Asked by companies like Amazon, this Active Users coding problem is a test of your ability to handle "Gaps and Islands" problems in SQL. Detecting consecutive patterns in data is a sophisticated skill that requires understanding row ordering, date arithmetic, and window functions.
The primary Database interview pattern used here involves DENSE_RANK() or ROW_NUMBER(). By subtracting a sequential row number from the login date, all dates in a consecutive sequence will result in the same "base date." You can then group by the User ID and this calculated "base date" to count the length of the streak.
Suppose a user logs in on Feb 1, Feb 2, Feb 3, and Feb 5.
DISTINCT or DENSE_RANK() can lead to counting multiple logins in one day as a "streak."DATE_SUB vs. PostgreSQL subtraction).Master the ROW_NUMBER() trick for consecutive sequences. It is the gold standard for "Gaps and Islands" problems and is much more scalable than any other iterative approach.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Customer Purchasing Behavior Analysis | Medium | Solve | |
| Employees With Deductions | Medium | Solve | |
| Find Interview Candidates | Medium | Solve | |
| Find the Missing IDs | Medium | Solve | |
| Grand Slam Titles | Medium | Solve |