Python Interview Prep/Weeks at Number One
Spotify logo

Weeks at Number One

Spotify·easy·pandas·About 10 min in the room

Spotify's weekly chart is in charts: one row per track per week, with its chart position. Find how many weeks each track spent at number one. Return a DataFrame result with the columns track and weeks, most weeks first, and tracks with the same number of weeks in alphabetical order. A track that never reached number one is left out.

The data

charts32 rows
weekint64positionint64trackobjectartistobject
11Neon RainJuno Vale
12Paper MoonsThe Ferrymen
13Low TideAma Reyes
14Glass HousesKit Marlowe
21Neon RainJuno Vale
22Glass HousesKit Marlowe
and 26 more rows

Target output

trackweeks
Neon Rain3
Paper Moons3
Glass Houses1
Low Tide1

What this question tests

Tests the core pandas loop: keep the rows that matter, count per group, then sort on two keys in two directions. It is the warm-up of many analyst screens because it shows in a minute whether you filter before you aggregate, and whether you know that a tie needs a rule.

More Python interview questions

  • The Salary Gap · LinkedIn · easy
  • Month-over-Month Revenue · Amazon · medium
  • Back Within a Week · Amazon · medium
  • Average Session Length · Meta · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...