Python Interview Prep/Popularity Percentage
Meta logo

Popularity Percentage

Meta·hard·pandas·About 25 min in the room

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
user1int64user2int64
12
13
23
41
52
61
and 4 more rows

Target output

userpopularity
144.444
244.444
333.333
422.222
522.222
622.222
711.111
811.111
911.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

  • Weeks at Number One · Spotify · easy
  • The Salary Gap · LinkedIn · easy
  • Month-over-Month Revenue · Amazon · medium
  • Back Within a Week · Amazon · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...