Imagine you have a Twitter-style social network.
Each post can have multiple tags.
Your task:
Return the top X most frequent tags used by your friends and your friends-of-friends.
In other words, starting from a given user:
- Look at all friends (1-hop)
- Look at all friends of friends (2-hop)
- Aggregate all tags from all posts of those users
- Return the Top X frequent tags
Example (conceptual):
User → friends: A, B
A’s tags: ["sports", "fun"]
B’s tags: ["news", "sports"]
A’s friends: C
C’s tags: ["travel", "sports"]
If X = 2, output:
["sports", "fun"] # For example (depends on actual counts)
用 BFS 遍历 1~2 层好友 ,收集所有标签;
使用哈希表统计频次;
排序或用小顶堆取前 X 个最常出现的标签。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完