The feature recommends a set of products that you have not already purchased, from the things that your friends have bought, in order from most bought to least bought.
Complete the function that takes in a person and returns the recommended products.
To help with this, we have the following 2 APIs that you can use in your solution:
List<Product> getPurchases(Person person);
Returns a list of products purchased by the person. If purchased multiple times, it will be in the list multiple times.
List<Person> getFriends(Person person);
Returns a list of friends of the person.
Provide your solution below.
This problem asks you to recommend products a person has not bought yet, based on what their friends have purchased, sorted from most frequently bought to least frequently bought. The key idea is to collect the person's existing purchases in a set for fast exclusion, then traverse all friends, count each friend's purchases with a hash map, and finally sort the unseen products by frequency in descending order. A set plus frequency counting is the natural data-structure choice here.