Problem · Hash Table
Directly Linked Users
Learn this problemProblem statement
Financial crimes are rarely committed in isolation. Bad actors may spread activity across multiple user accounts, but shared identifiers can reveal connections.
Each transaction log entry is formatted as user_id,device_id. Two different users are directly linked if they use the same device. A single user may appear multiple times with different devices.
Given the log and a target user, return all users directly linked to the target user. The target user should not appear in the output. Return the user IDs in lexicographic order.
Function
findDirectlyLinkedUsers(transactions: String[], targetUser: String) → String[]Complete findDirectlyLinkedUsers.
String transactions[n]: transaction rows formatteduser_id,device_idString targetUser: the user to investigate
Returns
String[]: directly linked users.
Examples
Example 1
transactions = ["Alice,D1","Bob,D1","Charlie,D2","David,D3","Eve,D1"]targetUser = "Alice"return = ["Bob","Eve"]Alice used D1. Bob and Eve also used D1.
Example 2
transactions = ["Alice,D1","Alice,D2","Bob,D2","Cara,D3"]targetUser = "Alice"return = ["Bob"]Alice is associated with D1 and D2, and Bob shares D2.
Constraints
- 0 <= transactions.length <= 10^5
- User IDs and device IDs contain no commas.