2
\$\begingroup\$

I was doing Favorite Genres code from leetcode

Question Link: https://leetcode.com/discuss/interview-question/373006

Question: Given a map Map> userSongs with user names as keys and a list of all the songs that the user has listened to as values.

Also given a map Map> songGenres, with song genre as keys and a list of all the songs within that genre as values. The song can only belong to only one genre.

The task is to return a map Map>, where the key is a user name and the value is a list of the user's favorite genre(s). Favorite genre is the most listened to genre. A user can have more than one favorite genre if he/she has listened to the same number of songs per each of the genres.

For this I wrote the following algorithm

let userSongs, songGenres


function popularity (users, geners) {
  const songsG = {}
  for (let gener in geners) {
    const songs = geners[gener]
    let i = 0
    while (i<songs.length) {
      const song = songs[i]
      songsG[song] = gener
      i++
    }
  }
  const pGeners = {}
  for (let user in users) {
    const uSongs = users[user]
    let i = 0; 
    const songMap = {}
    const pUserGeners = []
    while (i<uSongs.length) {
      const song = uSongs[i]
      const gener = songsG[song]
      if (songMap.hasOwnProperty(gener)) {
        if (songMap[gener] < 2) {
        pUserGeners.push(gener)
        songMap[gener] = songMap[gener] + 1
        }
      } else if (gener) {
        songMap[gener] = 1
      }
      i++
    }
     pGeners[user] = pUserGeners
  }
  return pGeners 
}

userSongs = {  
   "David": ["song1", "song2", "song3", "song4", "song8", "song11", "song12"],
   "Emma":  ["song5", "song6", "song7"]
},
songGenres = {  
   "Rock":    ["song1", "song3", "song11", "song12"],
   "Dubstep": ["song7"],
   "Techno":  ["song2", "song4"],
   "Pop":     ["song5", "song6"],
   "Jazz":    ["song8", "song9"]
}

console.log(popularity(userSongs, songGenres))

while the following code, does work, I am not thinking it is optimal. Can someone help me and suggest me on how I can make this code for optimize?

\$\endgroup\$
  • \$\begingroup\$ The code does not look right as described in the question you quoted from leetcode.. Users that listen at most once to any genre will not have any genre added to their list of favorite genres. You return an array of objects without user names rather than an object with users and genres. eg question wants map {David:["Rock","Techno"], Emma:["Pop"]}. you return array [{user:["Rock","Techno"]},{user:["Pop"]}] \$\endgroup\$ – Blindman67 Nov 8 at 3:18
  • \$\begingroup\$ Also the && songMap[gener] < 2 means that if the user listens to the same genre 4 times that genre is counted twice, 6 time and its counted three times, and so on.. \$\endgroup\$ – Blindman67 Nov 8 at 3:25
  • \$\begingroup\$ @iRohitBhatia Please provide the correct code (considering the above comments) \$\endgroup\$ – RomanPerekhrest Nov 8 at 6:04
  • \$\begingroup\$ @Blindman67 Fixed those bugs \$\endgroup\$ – iRohitBhatia Nov 8 at 12:38
  • \$\begingroup\$ Sorry to say but still not working, Still missing genres when only one song .and the logic you use to add genres is flawed, adding genres when more than one song even if not most fav genre. Maybe pull the question and comeback when you have given it a good test. I suggest you make up some data to test on not just the single test case you use. \$\endgroup\$ – Blindman67 Nov 8 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.