Amazon VO 面试真题解析:基于好友购买记录的商品推荐

15次阅读
没有评论

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.

这道题要求基于用户好友的购买记录,推荐用户尚未买过的商品,并且要按照“被好友购买的次数”从高到低排序。核心做法是先用 <code>getPurchases(person)</code> 获取当前用户已购买商品,放入集合中用于去重和快速判断;再通过 <code>getFriends(person)</code> 遍历所有好友,统计每个商品的出现次数,只保留当前用户没买过的商品。最后按统计频次降序输出即可。通常会结合哈希集合和哈希表来完成,时间复杂度主要取决于好友数量及其购买总量。

正文完
 0