In-Memory Communication Manager
Context
Design and implement a simple in-memory component responsible for tracking bidirectional communication links between users. This system operates within a single process. User IDs are assumed to be hashable (e.g., strings or integers).
Requirements
Implement the following API functions:
connect(user_a, user_b): Establishes a bidirectional communication link betweenuser_aanduser_b. Ifuser_acan communicate withuser_b, thenuser_bcan also communicate withuser_a.disconnect(user_a, user_b): Removes an existing bidirectional communication link betweenuser_aanduser_b, if one is present.clear(): Removes all existing user communication links from the system.
Additionally, you must:
- Describe the data structures used for your implementation.
- Provide code for the specified API functions.
- Analyze the time and space complexity of your solution.
Assumptions
- Connections are undirected and idempotent. Calling
connectmultiple times for the same pair of users should not create duplicate links or result in errors. - Self-connections (a user attempting to connect to themselves) should be rejected or ignored.
- Attempting to
disconnecta non-existent connection should be a no-op. - Creating a connection for users not yet known to the system should implicitly add them.
AI interview prep
Core dilemma
Optimizing in-memory data structures for efficient, dynamic management of undirected relationships, balancing lookup, insertion, and deletion performance with memory footprint, especially under varying graph densities.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
In-Memory Communication Manager
Context
Design and implement a simple in-memory component responsible for tracking bidirectional communication links between users. This system operates within a single process. User IDs are assumed to be hashable (e.g., strings or integers).
Requirements
Implement the following API functions:
connect(user_a, user_b): Establishes a bidirectional communication link betweenuser_aanduser_b. Ifuser_acan communicate withuser_b, thenuser_bcan also communicate withuser_a.disconnect(user_a, user_b): Removes an existing bidirectional communication link betweenuser_aanduser_b, if one is present.clear(): Removes all existing user communication links from the system.
Additionally, you must:
- Describe the data structures used for your implementation.
- Provide code for the specified API functions.
- Analyze the time and space complexity of your solution.
Assumptions
- Connections are undirected and idempotent. Calling
connectmultiple times for the same pair of users should not create duplicate links or result in errors. - Self-connections (a user attempting to connect to themselves) should be rejected or ignored.
- Attempting to
disconnecta non-existent connection should be a no-op. - Creating a connection for users not yet known to the system should implicitly add them.