In-Memory Communication Manager (Bidirectional Sessions)
Goal
Design a lightweight, in-memory manager that tracks bidirectional user-to-user sessions, similar to direct-message sessions. The manager must expose the following API functions:
connect(userA, userB): Establishes a bidirectional session betweenuserAanduserB.disconnect(userA, userB): Terminates the bidirectional session betweenuserAanduserB.clear(userA, userB): Removes any session state or history associated with the session betweenuserAanduserB, while keeping the users connected.
Requirements
- Specify method signatures in a language of your choice.
- Detail the key data structures used (e.g., hash maps, adjacency sets) and justify their selection.
- Provide the expected time and space complexities for each API function.
- Discuss edge cases such as duplicate connection attempts, a user attempting to connect to themselves, or operations on non-existent sessions. Describe how invalid inputs will be handled.
- Describe the approach to concurrency handling and the guarantees provided (e.g., thread safety, consistency).
- Include a brief testing plan covering the core functionality and edge cases.
Assumptions
- User IDs are represented as strings or integers.
- All data resides within a single process.
- No persistence is required; all session data is ephemeral and lives only in memory.
AI interview prep
Core dilemma
Balancing efficient concurrent access to shared mutable state (session data) with correctness and performance guarantees within a single-process, in-memory system. The challenge lies in managing the lifecycle and state of bidirectional sessions under high concurrency without introducing deadlocks, race conditions, or performance bottlenecks.
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 (Bidirectional Sessions)
Goal
Design a lightweight, in-memory manager that tracks bidirectional user-to-user sessions, similar to direct-message sessions. The manager must expose the following API functions:
connect(userA, userB): Establishes a bidirectional session betweenuserAanduserB.disconnect(userA, userB): Terminates the bidirectional session betweenuserAanduserB.clear(userA, userB): Removes any session state or history associated with the session betweenuserAanduserB, while keeping the users connected.
Requirements
- Specify method signatures in a language of your choice.
- Detail the key data structures used (e.g., hash maps, adjacency sets) and justify their selection.
- Provide the expected time and space complexities for each API function.
- Discuss edge cases such as duplicate connection attempts, a user attempting to connect to themselves, or operations on non-existent sessions. Describe how invalid inputs will be handled.
- Describe the approach to concurrency handling and the guarantees provided (e.g., thread safety, consistency).
- Include a brief testing plan covering the core functionality and edge cases.
Assumptions
- User IDs are represented as strings or integers.
- All data resides within a single process.
- No persistence is required; all session data is ephemeral and lives only in memory.