Count Unique Messenger Conversations Regardless of Direction
Last updated: November 29, 2026
Quick Overview
A Messenger messages table records sender and recipient for one-to-one chats. Count the number of unique conversations started each day, where a conversation is an unordered pair of users and its start is the first message between them.
Meta
November 29, 20260
0
0 solved
A Messenger messages table records sender and recipient for one-to-one chats. Count the number of unique conversations started each day, where a conversation is an unordered pair of users and its start is the first message between them.
This online assessment question in Meta's data science loop checks basic pair normalization. The interviewer wants LEAST and GREATEST to collapse A to B and B to A into one conversation and MIN to find its start.
What the Interviewer Expects
- Normalizes pairs with LEAST and GREATEST
- Finds the first message per pair with MIN
- Groups by the start date
- Excludes self messages
- Avoids COUNT(DISTINCT sender, recipient), which double counts
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you count conversations where both sides sent at least one message?
- How would you handle group threads?
- How would you count conversations per user?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
messages(message_id, sender_id, recipient_id, sent_at), one-to-one only. Conversation = unordered pair. Start date = date of the first message in either direction....
Approach
Map each message to (LEAST(sender, recipient), GREATEST(sender, recipient)). Group by that pair, take MIN(sent_at), then count pairs by the date of that minimum....