← 返回 meta 的题目列表Fix validation for friend recommendations
类型:online_judge
You are given a friend-recommendation setting with a User class:
User.id: an int or string unique identifier
User.currentFriends: list[User] representing existing friends
You are provided a buggy function valid_recommend(user, recommended_users) that validates whether the recommendations for user are legal. The current implementation does not pass the tests.
Fix valid_recommend so that it passes the tests and satisfies at least:
recommended_users is a list of User.
The recommendation list must not contain user themself.
(If covered by tests) The list must not include users already in user.currentFriends.
(If covered by tests) The list must not contain duplicates (treat same id as duplicate).
Modify only valid_recommend in the original file.
Constraints:
len(recommended_users) up to 1e5
The interviewer may ask for Big-O.
Example: user.id=1, recommended_users=[User(1), User(2)] => False (contains self)
Example
Input
(conceptual)
user.id=1, user.currentFriends=[]
recommended=[User(2),User(3)]
Output
True