ChatApp with Bots
Learn this problemProblem statement
Implement a small in-memory chat application that supports multiple channels and extensible bots.
A user sends messages to channels, registered bots may generate zero or more replies, and the application stores each channel's message history. Every channel must also have independent storage for stateful bot data. Adding a new bot type should require registering a new bot implementation, not changing the core message-processing logic.
For deterministic execution, process the following commands in order:
REGISTER botName1 [botName2 ...]: Register one or more example bots in the listed order. The runnable harness provides two bot types:- EchoBot: Returns the exact message text for every message.
- HelpBot: Returns
available commands: ...only when the complete message text is exactlyhelp. Matching is case-sensitive.
SEND channelId userId text...: AppenduserId: <text>to the named channel's history. The message text consists of all tokens afteruserId, joined by one space. Then invoke every registered bot in registration order. Each reply returned by a bot is appended asbotName: <reply>. A message may therefore produce zero, one, or multiple bot replies.PRINT channelId: Emit the complete history for that channel. The first emitted line ischannel=channelId, followed by the stored entries in insertion order. Printing a channel with no messages emits only its header.
Return the concatenation of every PRINT block in command order. Insert one empty string "" between consecutive blocks, with no empty string after the final block.
Function
runChatAppCommands(commands: String[]) → String[]Examples
Example 1
commands = ["REGISTER EchoBot HelpBot", "SEND A u1 hi", "SEND B u2 help", "PRINT A", "PRINT B"]return = ["channel=A", "u1: hi", "EchoBot: hi", "", "channel=B", "u2: help", "EchoBot: help", "HelpBot: available commands: ..."]Channel A receives hi, so EchoBot replies once. In channel B, the message help matches both registered bots. EchoBot first returns help, then HelpBot returns available commands: .... Both replies are stored in registration order. The empty string separates the two PRINT blocks.
Example 2
commands = ["REGISTER EchoBot", "SEND X u1 hello", "SEND X u2 world", "PRINT X"]return = ["channel=X", "u1: hello", "EchoBot: hello", "u2: world", "EchoBot: world"]EchoBot returns one reply for each message. The user messages and bot replies remain in insertion order inside channel X.
Constraints
1 <= commands.length <= 10^5- The number of distinct channels is at most
10^4. - Each channel contains at most
10^5user messages. - The extensible design must support up to
10^2registered bot instances. - The runnable command harness accepts
EchoBotandHelpBot. Each of these example bots appears at most once across allREGISTERcommands. channelIdanduserIdare non-empty strings with no whitespace.- Every
SENDcontains at least one message token, and command tokens are separated by one space. - Commands arrive sequentially. No concurrency handling is required.