In-Memory Communication Manager
Context
Build a simple in-memory component that tracks which users can communicate with each other. Treat a communication link between two users as an undirected connection (if A can talk to B, then B can talk to A). Assume user IDs are hashable (e.g., strings or integers) and that the system runs in a single process.
Requirements
Implement the following functions:
-
connect(user_a, user_b): Create a bidirectional connection so the two users can communicate.
-
disconnect(user_a, user_b): Remove an existing bidirectional connection, if present.
-
clear(): Remove all existing user communications.
Also:
-
Describe the data structures you use.
-
Provide code for the above API.
-
Analyze the time and space complexity.
Assumptions
-
Connections are undirected and idempotent (calling connect twice for the same pair should not create duplicates or errors).
-
Self-connections (a user connected to itself) should be rejected or ignored.
-
Disconnecting a non-existent connection should be a no-op.
-
Creating a connection for users not yet seen should implicitly add them to the system.