Communications Handler
Learn this problemProblem 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]callsconnectfor the two named callers.["hangup", user1, user2]callshangupfor the two named callers.["clear_all"]callsclear_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
user1anduser2are the same caller, the call raisesCommunicationExceptionwith the message{user1} cannot connect with {user2}. - If the callers are different but the line is already in use, the call raises
CommunicationExceptionwith the messageConnection in use. Please try later. - Otherwise, store the two callers as the active pair and return
Connection established between {user1} and {user2}.
Hangup
- If
user1anduser2are the same caller, the call raisesCommunicationExceptionwith 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
CommunicationExceptionwith 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"]AliceandBoboccupy the free channel.CarolandDavecannot connect while that pair remains active.hangupaccepts the active pair in reverse order and frees the channel.CarolandDavecan 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
100distinct 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
- Binary CircuitSeen Jul 2026
- Minimize Malware Spread by Removing a NodeOA · Seen Jul 2026
- Sort Array by FrequencyOA · Seen Jul 2026
- Array Challenge (QR Intern)OA · Seen Jul 2026
- K Smallest SubstringOA · Seen Jul 2026
- Maximum K-Star SumOA · Seen Jul 2026
- Delivery Management SystemOA · Seen Jul 2026
- Minimum SwapsOA · Seen Jul 2026