Facebook Heart-Reacted Posts
Problem
**[Asked at Meta]**
For each post, count the number of **distinct users who reacted with a heart**.
Return all posts sorted by heart count descending.
Return `title` and `heart_count`.
Input data
Example rows — the live problem includes the full dataset.
reactions
| reaction_id | user_id | post_id | reaction_type |
|---|---|---|---|
| 1 | 101 | 1 | heart |
| 2 | 102 | 1 | heart |
| 3 | 103 | 1 | like |
| 4 | 101 | 2 | heart |
| 5 | 102 | 2 | laugh |
posts
| post_id | author_id | title |
|---|---|---|
| 1 | 201 | Post One |
| 2 | 202 | Post Two |
| 3 | 201 | Post Three |
Expected output
Your answer should return 3 rows with the columns title, heart_count.
Starter code (Pandas (Python))
import pandas as pd
def heart_posts(reactions: pd.DataFrame, posts: pd.DataFrame) -> pd.DataFrame:
# Your code here
return reactionsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock