Implement a two-user communications handler
Akuna CapitalSoftware Engineer · Online AssessmentLast updated
This question evaluates skills in stateful resource management, API and abstraction design, custom exception handling, and input validation for enforcing a single-active connection between two users.
Design and implement a communications handler to manage a one-at-a-time conversation channel between exactly two distinct users. Specifications: (
- Define a custom exception type ConnectionException that accepts a single error-message string. (
- Define an abstract base CommsHandlerABC with three abstract methods: connect(user1: Caller, user2: Caller) -> str to establish a call or raise ConnectionException; hangup(user1: Caller, user2: Caller) -> str to end the call or raise ConnectionException; clear_all() -> None to force-clear any existing call. (
- Implement a concrete CommsHandler that tracks the current active call and enforces: only one active call at a time; a user cannot connect with themselves; while a call is active, any new connect attempt fails with an appropriate error; hangup succeeds only for the currently connected pair; clear_all resets state immediately. Caller has a unique name field (name: str). Process the following commands and produce the exact outputs shown. Sample Input: connect Alice Bob; connect Carol Dave; hangup Alice Bob; connect Carol Dave; hangup Carol Dave; connect Eve Eve; hangup Alice Bob. Expected Output: Success: Connection established between Alice and Bob; Error: Connection in use. Please try later; Success: Alice and Bob are disconnected; Success: Connection established between Carol and Dave; Success: Carol and Dave are disconnected; Error: Eve cannot connect with Eve; Error: Alice and Bob not found in the communication channel.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.