FastPrepFastPrep
Problem Brief

ChatApp with Bots

FULLTIMEPHONE SCREEN

Implement a small in-memory chat application that supports multiple channels and extensible bots.

Each channel maintains its own message history and any bot state associated with that channel. The design should allow new bot types to be added without modifying the core chat logic.

For this problem, process a list of commands:

  • REGISTER botName ...: register one or more bots
  • SEND channelId userId text...: append the user message to that channel and then append any bot replies triggered by the message
  • PRINT channelId: emit the full message history for that channel in order

Assume two example bots exist: EchoBot replies with the same text it receives, and HelpBot replies with a help string when the message text is help.

Function Description

Complete the function runChatAppCommands in the editor below.

runChatAppCommands has the following parameter:

  1. String[] commands: the commands to process in order

Returns

String[]: the lines produced by all PRINT commands, in order.

1Example 1

Input
commands = ["REGISTER EchoBot HelpBot", "SEND A u1 hi", "SEND B u2 help", "PRINT A", "PRINT B"]
Output
["channel=A", "u1: hi", "EchoBot: hi", "", "channel=B", "u2: help", "HelpBot: available commands: ..."]
Explanation

Each channel keeps an independent message log. EchoBot answers in channel A, while HelpBot answers in channel B.

2Example 2

Input
commands = ["REGISTER EchoBot", "SEND X u1 hello", "SEND X u2 world", "PRINT X"]
Output
["channel=X", "u1: hello", "EchoBot: hello", "u2: world", "EchoBot: world"]
Explanation

The same bot can respond multiple times inside the same channel, and channel history preserves insertion order.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= commands.length <= 10^5
  • The number of channels is at most 10^4.
  • The number of registered bots is at most 10^2.
  • Commands arrive sequentially; no concurrency handling is required.
public String[] runChatAppCommands(String[] commands) {
    // write your code here
}
Input

commands

["REGISTER EchoBot HelpBot", "SEND A u1 hi", "SEND B u2 help", "PRINT A", "PRINT B"]

Output

["channel=A", "u1: hi", "EchoBot: hi", "", "channel=B", "u2: help", "HelpBot: available commands: ..."]

Sign in to submit your solution.