Problem · Design

ChatApp with Bots

MediumOpenAIFULLTIMEPHONE 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.

Process a list of commands in order:

  • REGISTER botName1 [botName2 ...] — Register one or more bots by name. Multiple REGISTER commands 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 exactly help (case-sensitive).
  • SEND channelId userId text... — Append a user message entry (userId: <text>) to the named channel's history, where text is all tokens after userId joined 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 per SEND.
  • PRINT channelId — Emit the full message history for that channel. The first line emitted is channel=channelId, followed by each history entry in insertion order, one per line. A PRINT on a channel with no prior messages emits only the header line channel=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 REGISTER commands) is at most 102.
  • Bot names in REGISTER are drawn from the set {"EchoBot", "HelpBot"}.
  • channelId and userId are non-empty strings containing no whitespace.
  • Message text in SEND is at least one token; tokens are separated by single spaces.
  • Commands arrive sequentially; no concurrency handling is required.
More OpenAI problems
drafts saved locally
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