PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Akuna Capital
  • Coding & Algorithms
  • Software Engineer

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.

Input: ([('connect', 'Ann', 'Ben'), ('connect', 'Cara', 'Dan'), ('clear_all',), ('connect', 'Cara', 'Dan')],)

Expected Output: ['Connection established between Ann and Ben', 'Connection in use. Please try later', None, 'Connection established between Cara and Dan']

Explanation: The second connect fails because the line is busy. clear_all resets the line, allowing the later connect.

Input: ([('connect', 'Tom', 'Jerry'), ('hangup', 'Jerry', 'Tom'), ('hangup', 'Jerry', 'Jerry')],)

Expected Output: ['Connection established between Tom and Jerry', 'Jerry and Tom are disconnected', 'Jerry cannot hangup with Jerry']

Explanation: The active pair can hang up in either order. Hanging up with the same user name is invalid.

Input: ([],)

Expected Output: []

Explanation: With no operations, there are no recorded results.

Input: ([('connect', 'A', 'B'), ('hangup', 'C', 'D')],)

Expected Output: ['Connection established between A and B', None]

Explanation: Trying to hang up a pair that is not active does nothing and records None.

Hints

  1. You only need to remember one active communication pair at any time.
  2. Store the active pair in an order-independent form so that hangup('Alice', 'Bob') and hangup('Bob', 'Alice') match the same connection.
Last updated: Apr 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Graph Spread and Portfolio Trades - Akuna Capital (medium)
  • Find minimum swaps to sort array with duplicates - Akuna Capital (hard)
  • Heapify an array into a max-heap - Akuna Capital (Medium)
  • Implement a ring buffer - Akuna Capital (Medium)
  • Compute max profit across dated stock quotes - Akuna Capital (Medium)