Design two-user communications handler with exceptions
Company: Akuna Capital
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: HR Screen
Implement a two-user communications handler. Create:
(A) CommsHandler(CommsHandlerABC) with methods connect(self, user1: Caller, user2: Caller) -> str, hangup(self, user1: Caller, user2: Caller) -> str, and clear_all(self) -> None; and
(B) CommunicationException(Exception) that accepts a message string. Behavior: Only two users can communicate at any given time; no other users can connect until the current communication ends. connect: If user1 is the same as user2, raise CommunicationException with the message "{user1.name} cannot connect with {user2.name}". If user1 and user2 are different and the line is in use, raise CommunicationException("Connection in use. Please try later"). If the line is free, store the active pair and return "Connection established between {user1.name} and {user2.name}". hangup: If user1 is the same as user2, raise CommunicationException with the message "{user1.name} cannot hangup with {user2.name}". If user1 and user2 are different and currently communicating, disconnect them and return "{user1.name} and {user2.name} are disconnected". clear_all: Clear the communication channel regardless of current users. Assume a provided Caller class with a name attribute.
Quick Answer: This question evaluates object-oriented design, state management, and custom exception handling skills for enforcing a single active communication pair and related invariants between two users.
Implement the behavior of a two-user communications handler as a function-based simulator. In the original object-oriented version, invalid actions would raise a CommunicationException with a specific message. For this problem, instead of raising, append that exact message to the result list and continue processing the remaining operations.
Each caller is represented by a string name.
You are given a list of operations. Each operation is one of:
- ('connect', user1, user2)
- ('hangup', user1, user2)
- ('clear_all',)
Rules:
1. Only one pair of users can communicate at a time.
2. connect:
- If user1 == user2, record '{user1} cannot connect with {user2}'.
- Else if the line is already in use, record 'Connection in use. Please try later'.
- Else store the active pair and record 'Connection established between {user1} and {user2}'.
3. hangup:
- If user1 == user2, record '{user1} cannot hangup with {user2}'.
- Else if user1 and user2 are the currently active pair in any order, disconnect them and record '{user1} and {user2} are disconnected'.
- Otherwise, record None and leave the state unchanged.
4. clear_all:
- Clear the active communication no matter who is connected.
- Record None.
Return the list of recorded results in order.
Constraints
- 0 <= len(operations) <= 100000
- Each user name is a non-empty string with length up to 50
- Operation names are always one of 'connect', 'hangup', or 'clear_all'
- User name comparisons are case-sensitive
Examples
Input: ([('connect', 'Alice', 'Bob'), ('hangup', 'Alice', 'Bob')],)
Expected Output: ['Connection established between Alice and Bob', 'Alice and Bob are disconnected']
Explanation: The pair connects successfully, then the same pair disconnects.
Input: ([('connect', 'Eve', 'Eve')],)
Expected Output: ['Eve cannot connect with Eve']
Explanation: A user cannot connect to themself, so the exception message is recorded.
Hints
- You only need to remember one active communication pair at any time.
- Store the active pair in an order-independent form so that hangup('Alice', 'Bob') and hangup('Bob', 'Alice') match the same connection.