507. Design System to Count Song Upvotes
Design System to Count Song Upvotes

Design a system that counts song upvotes and returns the top k most upvoted songs for a given artist.

Each song is identified by its songName together with its artistName. Different artists can have songs with the same name, and these are different songs.

The songs of an artist are ranked as follows:

  • A song with more votes comes first.
  • If two songs have the same number of votes, the lexicographically smaller songName comes first.

Methods

Constructor

SongVoteTracker(int k)
  • k is the maximum number of songs that getTopKSongs returns.

Upvote a Song

void upvoteSongByOne(String songName, String artistName)
  • Adds one vote to the song songName by the artist artistName.
  • If the song has not been upvoted before, it is added with 1 vote.

Get the Top K Songs

List<String> getTopKSongs(String artistName)
  • Returns the names of the top k songs of artistName, in ranking order.
  • If the artist has fewer than k upvoted songs, returns all of them in ranking order.
  • If no song of artistName has been upvoted, returns an empty list.

Constraints

  • 1 ≤ k ≤ 100
  • 1 ≤ songName.length(), artistName.length() ≤ 20
  • songName and artistName contain only lowercase English letters.
  • At most 100,000 calls in total will be made to upvoteSongByOne and getTopKSongs.
  • At most 10,000 of these calls will be made to getTopKSongs.

Examples

Example 1

SongVoteTracker(k = 2)
upvoteSongByOne(songName = "sunrise", artistName = "mira")
upvoteSongByOne(songName = "ocean", artistName = "mira")
getTopKSongs(artistName = "mira") returns ["ocean", "sunrise"]
upvoteSongByOne(songName = "sunrise", artistName = "mira")
upvoteSongByOne(songName = "lanterns", artistName = "mira")
getTopKSongs(artistName = "mira") returns ["sunrise", "lanterns"]
upvoteSongByOne(songName = "ocean", artistName = "mira")
getTopKSongs(artistName = "mira") returns ["ocean", "sunrise"]
getTopKSongs(artistName = "kabir") returns []

Explanation: At first, "ocean" and "sunrise" both have 1 vote, so the smaller name "ocean" comes first. Next, "sunrise" reaches 2 votes, while "lanterns" and "ocean" have 1 vote each. Only 2 songs are returned, so "lanterns" is picked over "ocean" because its name is smaller. Then "ocean" reaches 2 votes, ties with "sunrise" and comes first again. No song of "kabir" has been upvoted, so the last list is empty.

Example 2

SongVoteTracker(k = 3)
upvoteSongByOne(songName = "rain", artistName = "nora")
upvoteSongByOne(songName = "rain", artistName = "nora")
upvoteSongByOne(songName = "rain", artistName = "leo")
upvoteSongByOne(songName = "home", artistName = "leo")
upvoteSongByOne(songName = "home", artistName = "leo")
getTopKSongs(artistName = "leo") returns ["home", "rain"]
getTopKSongs(artistName = "nora") returns ["rain"]

Explanation: "rain" by "nora" has 2 votes, and "rain" by "leo" is a different song with 1 vote. "home" by "leo" has 2 votes, so it comes first. "leo" has only 2 upvoted songs, fewer than k = 3, so both are returned.



Please use Laptop/Desktop or any other large screen to add/edit code.