← Questions

Count active follow connections in SQL

0
MetaCodingLast asked Aug 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Aug 2025. It tests sql and data engineering.

A table logs follow events between users over time. Write SQL that returns how many follow connections are active right now. A connection from user_id to target_id is active when its latest follow_success has not been undone by a later unfollow.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • Table events(user_id INT, target_id INT, event_type VARCHAR, event_time TIMESTAMP).
  • event_type is one of request_follow, follow_success, follow_reject, unfollow.
  • Only follow_success and unfollow change the state of a pair. Return one row with the count of active (user_id, target_id) pairs.

Example

user_id  target_id  event_type       event_time
1        2          request_follow   2025-05-01 10:00
1        2          follow_success   2025-05-01 10:05      # active
1        3          request_follow   2025-05-02 09:00
1        3          follow_reject    2025-05-02 09:30      # never active
2        1          follow_success   2025-05-03 12:00
2        1          unfollow         2025-05-10 08:00      # undone
3        1          follow_success   2025-05-04 12:00
3        1          unfollow         2025-05-05 12:00
3        1          follow_success   2025-05-20 12:00      # followed again: active
1        4          follow_success   2025-05-06 10:00
1        4          request_follow   2025-05-07 10:00      # does not change the state: active

active_connections
3

Clarifying questions

  • Is a follow directed? Do (1, 2) and (2, 1) count separately?
  • Which events change the state? Can a request_follow after a success matter?
  • Can two events of one pair share a timestamp? Which wins?
  • "Now" is a fixed date or the latest data?

What a strong answer covers

  • Filter to the two state-changing events first, then take the latest per pair with ROW_NUMBER() OVER (PARTITION BY user_id, target_id ORDER BY event_time DESC) and count pairs whose latest event is follow_success.
  • The alternative: compare MAX(event_time) of successes and unfollows per pair with conditional aggregation.
  • The pitfall of ranking all event types: a later request_follow or follow_reject would hide an active connection.
  • Ties at the same timestamp, and an index on (user_id, target_id, event_time).

Follow-ups

  • Active connections at the end of every day for the past month.
  • The number of active followers per user, and the top 10 users.
  • Mutual connections: pairs active in both directions.

Where it's asked

  • Meta · AI-enabled coding · Last asked Aug 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

sql · data engineering

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding