← 返回 meta 的题目列表Implement mutual friends computation with tests
类型:online_judge
Implement mutual friends in a friend graph.
Given a User class:
id
currentFriends: list[User]
In a new file, implement:
def mutual_friends(user_a: User, user_b: User) -> list[User]:
"""Return the mutual friends of user_a and user_b (dedup by id)."""
Requirements:
Mutual friends are users present in both user_a.currentFriends and user_b.currentFriends (compare by id).
Output must have no duplicates.
Any order is acceptable, but document your ordering choice in tests (e.g., sort by id).
Write a test file that covers: none, multiple mutuals, duplicate entries in friend lists, and empty lists.
Constraints:
len(currentFriends) up to 1e5
Example: A friends [2,3,4], B friends [3,4,5] => [3,4]
Example
Input
A friends=[2,3,4], B friends=[3,4,5]
Output
[3,4]