← Bytedance Interview Insights
I knew the general shape of the answer (date subtraction, row_number trick) but fumbled explaining it out loud.
Start by clarifying the schema and assumptions (e.g., one row per user per day, date format). Then explain the window function approach: use LAG or LEAD to compare each login date with the previous/next login date, and filter where the difference is exactly 1 day. Finally, discuss edge cases like multiple logins per day and timezone considerations.
Pro tip: Mention that you'd deduplicate dates per user first (using DISTINCT or GROUP BY) to avoid false positives from multiple logins on the same day, and note that window functions like LAG are standard in modern SQL dialects (e.g., MySQL 8+, PostgreSQL).
Ask about the table schema, whether each row represents a unique user-day, and the date format. Confirm that 'consecutive days' means calendar days, not 24-hour periods.
Use a subquery or CTE to get distinct (user_id, login_date) pairs, ensuring multiple logins on the same day don't skew the analysis.
Use LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) to get the previous login date for each row.
Select rows where the difference between the current login_date and the previous login_date is exactly 1 day (e.g., DATEDIFF(day, prev_date, login_date) = 1).
Return DISTINCT user_id. Optionally, mention alternative approaches like self-join or using LEAD, and discuss performance considerations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.