Be the first user to complete this post

  • 0
Add to List
Beginner

500. Print Top 10 videos from List

Given a list L of video names and their watch rates, write a function that will return the videos with the top 10 watch rates. Video names may appear more than once.

Example:

Input:
[abc : 10] [def : 20] [abc : 15] [ghi : 50] [xyz : 100] [abc : 25] [jkl : 10] [mno : 15] [pqr : 25] [stu : 35] [lko : 19] [aaa : 5] [bbb : 10] [ccc : 35] [abc : 25] [eee : 20]
Output:
Top videos:
[xyz : 100] [abc : 75] [ghi : 50] [ccc : 35] [stu : 35] [pqr : 25] [def : 20] [eee : 20] [lko : 19] [mno : 15]

Approach:

  • Construct the map from the given list of Video with videoName as key and its watch rate. With this the watch rates will be combined for each video.
  • Convert the constructed map to the list of type Map.Entry<String, Integer>.
  • Sort the list by overriding the compare function in Comparator. Inside compare function - compare the map values.
  • Iterate the list and print the top 10 items from the list.

Output:

Top videos:
[xyz : 100] [abc : 75] [ghi : 50] [ccc : 35] [stu : 35] [pqr : 25] [def : 20] [eee : 20] [lko : 19] [mno : 15]

Reference: https://www.careercup.com/question?id=5713593060818944