Practice / window functions

Chart-Toppers

hard

Waveform wants a per-genre leaderboard: the two most-played tracks in every genre.

track_plays

Your task: return genre, title, and plays for the two most-played tracks in each genre. If two tracks in a genre tie on plays, break the tie by title (A→Z) and keep the earlier one — so every genre returns exactly two rows.

Assign your answer DataFrame to result. Row order doesn't matter.

track_playsinput DataFrame

Schema
columntype
titlestring
genrestring
playslong
Sample rows
titlegenreplays
Neon Skylinesynth900
Undertowsynth750
Riptidesynth750
Paper Moonssynth500
Marlow's Themeindie610
Slow Staticindie420
Gravel Roadindie300
Expected output shape
genretitleplays· 4 rows
Hint

Window.partitionBy("genre").orderBy(F.col("plays").desc(), F.col("title").asc()). Add F.row_number().over(w), keep rn <= 2, then select("genre", "title", "plays"). The title tiebreaker makes the pick deterministic when two tracks share a play count.

Lesson refresher

This problem builds on Window Functions (~9 min). Pop it open in a new tab if you want a quick recap.

Loading editor…
Hit Run to execute your code and see the output here.