Problem · Design
ChatApp with Bots
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.
Process a list of commands in order:
REGISTER botName1 [botName2 ...]— Register one or more bots by name. MultipleREGISTERcommands accumulate; re-registering an already-registered bot has no effect. Two built-in bot types exist:- EchoBot — replies with the exact message text it receives, for every message.
- HelpBot — replies with the string
available commands: ...only when the entire message text is exactlyhelp(case-sensitive).
SEND channelId userId text...— Append a user message entry (userId: <text>) to the named channel's history, wheretextis all tokens afteruserIdjoined with a single space. Then, in registration order, check each registered bot: the first bot whose trigger condition matches the message text appends its reply (botName: <reply>) to that channel's history. At most one bot replies perSEND.PRINT channelId— Emit the full message history for that channel. The first line emitted ischannel=channelId, followed by each history entry in insertion order, one per line. APRINTon a channel with no prior messages emits only the header linechannel=channelId.
The return value is the concatenation of all lines produced by all PRINT commands, in order. When multiple PRINT commands are issued, the output blocks are separated by a single empty string "" between consecutive blocks (i.e., after every block except the last).
Examples
01 · 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", "HelpBot: available commands: ..."]
Channel A receives 'hi' from u1; EchoBot (registered first) matches every message and replies 'hi'. Channel B receives 'help' from u2; HelpBot matches exactly 'help' and replies 'available commands: ...'; EchoBot does not reply because HelpBot was the first matching bot. The two PRINT blocks are separated by a single empty string '""' between them (but no trailing empty string after the last block).
02 · 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"]
The same bot can respond multiple times inside the same channel, and channel history preserves insertion order.
Constraints
Constraints:
1 <= commands.length <= 105- The number of distinct channels is at most
104. - The number of registered bots (across all
REGISTERcommands) is at most102. - Bot names in
REGISTERare drawn from the set{"EchoBot", "HelpBot"}. channelIdanduserIdare non-empty strings containing no whitespace.- Message text in
SENDis at least one token; tokens are separated by single spaces. - Commands arrive sequentially; no concurrency handling is required.
More OpenAI problems
- Grid Infection Spread Until StablePHONE SCREEN · Seen May 2026
- Grid Infection with Immune Cells Until StablePHONE SCREEN · Seen May 2026
- Infection Spread / Cellular AutomataPHONE SCREEN · Seen May 2026
- Chat Event Counts in Recent WindowPHONE SCREEN · Seen May 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen May 2026
- IP Address to CIDR BlocksPHONE SCREEN · Seen May 2026
- Count Valid SequencesSeen Jan 2025
- Maximize The HitsSeen Sep 2024
public String[] runChatAppCommands(String[] commands) {
// write your code here
}
commands["REGISTER EchoBot HelpBot", "SEND A u1 hi", "SEND B u2 help", "PRINT A", "PRINT B"]
expected["channel=A", "u1: hi", "EchoBot: hi", "", "channel=B", "u2: help", "HelpBot: available commands: ..."]
sign in to submit