Query Top Played Tracks Globally And By Country
Company: Spotify
Role: Machine Learning Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Technical Screen
Given a `play_events` table with columns `user_id`, `track_id`, `timestamp`, and `country`, write SQL to return the top N most played tracks globally and the top N most played tracks per country.
<details>
<summary>Hint 1</summary>
Start by naming the core entities, constraints, and success criteria.
</details>
<details>
<summary>Hint 2</summary>
Make the trade-offs explicit before going deep on implementation details.
</details>
### Constraints & Assumptions
- Each row represents one playback event.
- A track can be played many times by the same user.
- The query should count plays, not distinct users, unless clarified otherwise.
- Use deterministic tie-breaking if the interviewer asks for stable output.
### Clarifying Questions to Ask
- Should repeated plays by the same user count multiple times?
- Should the result be filtered to a date range?
- How should ties at rank N be handled?
- Should countries with fewer than N tracks return fewer rows?
- What SQL dialect should be used?
### What a Strong Answer Covers
```premium-lock What a Strong Answer Covers
```
### Follow-up Questions
- How would you return all tracks tied at Nth place?
- How would you compute top N by unique listeners instead of plays?
- How would you optimize for a daily dashboard?
- What changes if events arrive late?
Quick Answer: Practice SQL for music playback analytics by finding top tracks globally and top tracks per country from event logs. The prompt targets aggregation, window functions, tie behavior, deterministic ordering, and dashboard-oriented performance considerations.