friends lists friendships as pairs (user1, user2). Each friendship appears once, in either order, and every user is in at least one pair. A user's popularity is their number of friends as a percentage of all the users on the platform. Return result with the columns user and popularity (rounded to 3 decimals), one row per user, ordered by user.
The data
friends10 rows
| user1int64 | user2int64 |
|---|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 4 | 1 |
| 5 | 2 |
| 6 | 1 |
and 4 more rows
Target output
| user | popularity |
|---|
| 1 | 44.444 |
| 2 | 44.444 |
| 3 | 33.333 |
| 4 | 22.222 |
| 5 | 22.222 |
| 6 | 22.222 |
| 7 | 11.111 |
| 8 | 11.111 |
| 9 | 11.111 |
What this question tests
Tests treating an undirected relationship correctly: a friendship stored once has to be counted from both ends. Graph-shaped data in a table is a staple of social-network interviews, and the one-sided count is the answer most people give first.
More Python interview questions