KatInterview

Monthly Hit Tracks: Overall and Per-Region Leaderboards

Arrays & HashingAuto-graded · 16 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingsorting

Last updated

Filter streaming events by duration and date window, count plays per track, and produce a global top 5 and per-country top 3 with deterministic tie-breaking.

Monthly Hit Tracks

A music app records every listening session as an event. You are building a popularity report for January 2024 that covers the whole world and each individual country.

Write a function monthly_top_tracks(events). The argument events is a list of dictionaries. Each dictionary describes one listening event and may contain these keys:

  • event_id: unique identifier of the event
  • user_id: the listener
  • track_id: the track that was played (integer, may be missing or None)
  • country_code: two-letter country code (string, may be missing or None)
  • played_at: when the play happened, either an ISO-style string such as "2024-01-05 12:00:00" or a datetime object (may be missing or None)
  • duration_secs: how many seconds were listened to (integer, may be missing or None)

What counts as a valid play

An event is counted only if all of the following hold:

  • duration_secs is present and is at least 30 (exactly 30 counts, 29 does not).
  • played_at is present and lies between 2024-01-01 00:00:00 and 2024-01-31 23:59:59, with both ends included.
  • track_id is present (not None).
  • country_code is present (not None).

Events that fail any of these checks are ignored for both results below.

What to return

Return a dictionary with exactly two keys:

  1. "global_top_5": a list of [track_id, play_count] pairs for the 5 tracks with the most valid plays worldwide. Sort by play_count descending; if counts are equal, the smaller track_id comes first. If fewer than 5 tracks have valid plays, return all of them.

  2. "country_top_3": a list of [country_code, track_id, play_count, rank] rows. For each country, take the 3 tracks with the most valid plays in that country, using the same ordering (count descending, then track_id ascending), and give them ranks 1, 2, 3 in that order. Ranks are always consecutive integers; tied tracks do not share a rank. If a country has fewer than 3 distinct played tracks, include all of them. Rows are ordered by country_code ascending, then rank ascending.

If no valid plays exist, both lists are empty.

Constraints

  • 0 <= len(events) <= 10^5
  • track_id values are integers; country_code values are strings.
  • played_at strings are parseable by datetime.fromisoformat.
  • Ties are always resolved by the smaller track_id.
  • Counting is per valid event; each event contributes at most one play.

Examples

Example 1

Input: monthly_top_tracks([{'event_id': 1, 'user_id': 10, 'track_id': 1042, 'country_code': 'BR', 'played_at': '2024-01-05 12:00:00', 'duration_secs': 45}, {'event_id': 2, 'user_id': 11, 'track_id': 2011, 'country_code': 'BR', 'played_at': '2024-01-06 12:00:00', 'duration_secs': 35}, {'event_id': 3, 'user_id': 12, 'track_id': 105, 'country_code': 'SE', 'played_at': '2024-01-10 09:30:00', 'duration_secs': 30}, {'event_id': 4, 'user_id': 13, 'track_id': 1042, 'country_code': 'BR', 'played_at': '2024-01-31 23:59:59', 'duration_secs': 60}, {'event_id': 5, 'user_id': 14, 'track_id': 9901, 'country_code': 'SE', 'played_at': '2024-02-01 00:00:00', 'duration_secs': 50}, {'event_id': 6, 'user_id': 15, 'track_id': 451, 'country_code': 'BR', 'played_at': '2024-01-15 08:00:00', 'duration_secs': 29}])

Output: {'global_top_5': [[1042, 2], [105, 1], [2011, 1]], 'country_top_3': [['BR', 1042, 2, 1], ['BR', 2011, 1, 2], ['SE', 105, 1, 1]]}

Hints

Hint 1

Decide first which events are valid. Do all the filtering (duration, date window, missing track or country) in one pass before thinking about ranking.

Hint 2

While scanning, keep one counter keyed by track for the global result and one counter per country keyed by track for the regional result.

Hint 3

Sort each counter's items with the key (-count, track_id) and slice to 5 or 3. Assign ranks with enumerate starting at 1, and iterate countries in sorted order.