You are given a list of user records, a map of similarity weights per field, a target user ID and a similarity threshold. Find every user who is linked to the target user.
The similarity score between two users is computed field by field: for each field in weights, if both users have the same value for that field, the field's weight is added to the score. Two users are directly linked when their score is greater than or equal to threshold.
Links are transitive. If user A is linked to user B and user B is linked to user C, then A is linked to C through B, even when A and C score below the threshold against each other. Return the sorted list of all user IDs linked to the target, directly or indirectly, excluding the target itself.
Function Signature
def find_linked_users(users: list[dict], weights: dict[str, int], target_id: int, threshold: int) -> list[int]:
Rules
-
Each user is a dict with an integer
"id"
key and string-valued fields, for example
{"id": 1, "city": "NYC", "lang": "Python"}
.
-
Only fields named in
weights
count toward the score. A field adds its weight only when both users have that field and the two values are equal.
-
The score is symmetric, so if A is linked to B then B is linked to A.
-
Return the linked IDs in ascending order. Return an empty list when no user is linked to the target.
Constraints
-
1 <= len(users) <= 100
-
Each user has a unique integer
id
and string-valued fields
-
Weights are non-negative integers
-
0 <= threshold <= 1000
-
target_id
is guaranteed to exist in
users
Examples
Input:
users = [
{"id": 1, "city": "NYC", "lang": "Python", "role": "engineer"},
{"id": 2, "city": "NYC", "lang": "Python", "role": "designer"},
{"id": 3, "city": "LA", "lang": "Java", "role": "designer"},
{"id": 4, "city": "LA", "lang": "Java", "role": "engineer"},
{"id": 5, "city": "NYC", "lang": "Java", "role": "manager"}
]
weights = {"city": 3, "lang": 5, "role": 2}
target_id = 1
threshold = 5
Output: [2]
Users 1 and 2 share city and lang, so they score 3 + 5 = 8 and are linked. User 1 scores only 2 against user 4 and 3 against user 5, and user 2 scores at most 3 against users 3, 4 and 5. Users 3, 4 and 5 are linked to one another through lang, but that group never connects back to users 1 and 2.
Input:
users = [
{"id": 10, "dept": "sales", "level": "senior"},
{"id": 20, "dept": "sales", "level": "junior"},
{"id": 30, "dept": "eng", "level": "junior"},
{"id": 40, "dept": "eng", "level": "senior"}
]
weights = {"dept": 4, "level": 3}
target_id = 10
threshold = 3
Output: [20, 30, 40]
User 10 is directly linked to user 20 (same dept, score 4) and to user 40 (same level, score 3). Users 10 and 30 share nothing, but user 30 is linked to user 20 through level and to user 40 through dept, so it is reached indirectly.
Input:
users = [
{"id": 7, "team": "core"},
{"id": 8, "team": "infra"}
]
weights = {"team": 4}
target_id = 7
threshold = 1
Output: []
The two users have different team values, so their score is 0, which is below the threshold. User 7 has no direct link, so it has no indirect ones either.