The part that trips people up is the 'no subsequent unfollow' condition.
Clarify the definition of an active follow connection and the grain of the events table, then outline a SQL strategy that identifies the latest relevant event per user-target pair and filters for 'follow_success'. Use window functions or aggregation to determine the current state, and discuss edge cases like multiple follows/unfollows and null handling.
Pro tip: Mention that you would validate the query against sample data and consider performance implications, such as indexing on (user_id, target_id, event_time) and using incremental processing if the table is large.
Confirm that 'active' means the most recent event for a user-target pair is 'follow_success', and that 'unfollow' is the only event that deactivates a connection. Ask about data volume, update frequency, and whether historical states are needed.
Use a window function like ROW_NUMBER() OVER (PARTITION BY user_id, target_id ORDER BY event_time DESC) to rank events, or use aggregation with MAX(event_time) to find the most recent event for each pair.
Select pairs where the latest event_type is 'follow_success'. This ensures no subsequent 'unfollow' has occurred.
Count the distinct user-target pairs. Consider edge cases: multiple events at the same timestamp, missing events, and whether 'follow_reject' or 'request_follow' affect the state (they do not, per definition).
Compose the final SQL, using CTEs for readability. Discuss indexing and partitioning strategies for large-scale data, and mention potential incremental computation if the table is append-only.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.