Problem · Design

Communications Handler

Learn this problem
MediumAkuna CapitalNew GradOA

Problem statement

Implement a communications handler for a single shared communication channel. At most two callers can communicate at a time, and no other pair can connect until the current communication ends.

The input instructions describes method calls in chronological order:

  • ["connect", user1, user2] calls connect for the two named callers.
  • ["hangup", user1, user2] calls hangup for the two named callers.
  • ["clear_all"] calls clear_all.

Caller names uniquely identify the corresponding Caller objects. Treat a connected pair as unordered, so either caller order refers to the same active communication.

Connect

  • If user1 and user2 are the same caller, the call raises CommunicationException with the message {user1} cannot connect with {user2}.
  • If the callers are different but the line is already in use, the call raises CommunicationException with the message Connection in use. Please try later.
  • Otherwise, store the two callers as the active pair and return Connection established between {user1} and {user2}.

Hangup

  • If user1 and user2 are the same caller, the call raises CommunicationException with the message {user1} cannot hangup with {user2}.
  • If the two callers are the active pair, disconnect them and return {user1} and {user2} are disconnected.
  • Otherwise, the call raises CommunicationException with the message {user1} and {user2} not found in the communication channel.

Clear All

clear_all clears the channel regardless of its current users.

Return an array containing, in order, the returned string or exception message from every connect and hangup instruction. A clear_all instruction produces no output entry.

Function

handleCommunications(instructions: String[][]) → String[]

Examples

Example 1

instructions = [["connect","Alice","Bob"],["connect","Carol","Dave"],["hangup","Bob","Alice"],["connect","Carol","Dave"]]return = ["Connection established between Alice and Bob","Connection in use. Please try later","Bob and Alice are disconnected.","Connection established between Carol and Dave"]
  1. Alice and Bob occupy the free channel.
  2. Carol and Dave cannot connect while that pair remains active.
  3. hangup accepts the active pair in reverse order and frees the channel.
  4. Carol and Dave can then connect.

Example 2

instructions = [["connect","Mia","Mia"],["hangup","Mia","Noah"],["connect","Mia","Noah"],["hangup","Mia","Mia"],["hangup","Noah","Mia"]]return = ["Mia cannot connect with Mia","Mia and Noah not found in the communication channel.","Connection established between Mia and Noah","Mia cannot hangup with Mia","Noah and Mia are disconnected."]

The rejected same-caller calls do not change the channel. The final reversed-order hangup matches the active pair and disconnects it.

Example 3

instructions = [["connect","Ava","Ben"],["clear_all"],["hangup","Ava","Ben"],["connect","Cara","Dan"]]return = ["Connection established between Ava and Ben","Ava and Ben not found in the communication channel.","Connection established between Cara and Dan"]

clear_all removes Ava and Ben without adding an output entry. Their later hangup is therefore not found, and the channel remains available for Cara and Dan.

Constraints

  • 1 <= instructions.length <= 2000.
  • At most 100 distinct caller names appear.
  • Every instruction is one of ["connect", user1, user2], ["hangup", user1, user2], or ["clear_all"].
  • Every caller name is a non-empty, case-sensitive string that uniquely identifies one caller.

More Akuna Capital problems

drafts saved locally
public String[] handleCommunications(String[][] instructions) {
    // write your code here
}
instructions[["connect","Alice","Bob"],["connect","Carol","Dave"],["hangup","Bob","Alice"],["connect","Carol","Dave"]]
expected["Connection established between Alice and Bob", "Connection in use. Please try later", "Bob and Alice are disconnected.", "Connection established between Carol and Dave"]
checking account