Problem · Design

ChatApp with Bots

Learn this problem
MediumOpenAI logoOpenAIFULLTIMEPHONE SCREEN

Problem 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 exactly help. Matching is case-sensitive.
  • SEND channelId userId text...: Append userId: <text> to the named channel's history. The message text consists of all tokens after userId, joined by one space. Then invoke every registered bot in registration order. Each reply returned by a bot is appended as botName: <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 is channel=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^5 user messages.
  • The extensible design must support up to 10^2 registered bot instances.
  • The runnable command harness accepts EchoBot and HelpBot. Each of these example bots appears at most once across all REGISTER commands.
  • channelId and userId are non-empty strings with no whitespace.
  • Every SEND contains at least one message token, and command tokens are separated by one space.
  • 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", "EchoBot: help", "HelpBot: available commands: ..."]
checking account