KatInterview

Multi-Channel Message Router with Pluggable Responders

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdesignsimulationoop

Last updated

Simulate a command-driven chat engine that dispatches messages to registered responders, keeps per-channel logs and per-responder state, and returns the resulting logs.

You are building a small in-memory chat engine and a driver function that runs a scripted sequence of commands against it. The engine routes each incoming message to every registered responder ("bot"), and keeps an independent history for each channel.

Implement simulate_chat(commands, params). The two lists have the same length; commands[i] is the name of a command and params[i] is the list of its arguments. Process the commands in order and return a list with one result per command.

Commands

register_counter, params [bot_id] Registers a counter bot under the identifier bot_id. Result: None.

register_keyword, params [bot_id, keyword, reply] Registers a keyword bot under bot_id. Result: None.

send, params [channel_id, sender, text] Delivers a user message to the channel channel_id, then returns the full log of that channel (see below).

get_log, params [channel_id] Returns the current log of the channel without changing anything.

Behavior

  • Bots are global: every registered bot sees messages from every channel. Bots are consulted in the order they were registered.
  • A channel springs into existence the first time a send targets it. Before that, get_log on it returns an empty list [], and get_log must not create the channel.
  • When a message is sent, first append the user message to the channel's log. Then, for each registered bot in registration order, give the bot the original user message together with that bot's own state for that channel. If the bot produces a reply, append the reply to the log immediately. Bot replies are not passed to other bots.
  • Each bot's state is kept separately per (channel, bot_id) pair, starting out empty. A bot registered after a channel already exists simply starts with empty state in that channel.
  • Registering a bot never changes any existing log.

Bot types

Counter bot. It replies to every message. It keeps a count in its state for the channel, starting at 0. On each message it increments the count first, then replies with sender "counter" and text "Message N: T", where N is the new count and T is the text of the user message.

Keyword bot. It is configured with a keyword and a reply. It replies only if keyword appears in the message text, compared case-insensitively (substring match). The reply has sender "keyword" and text equal to reply. Otherwise it stays silent. It does not need any state.

Log format

A log is a list of dictionaries, one per entry in chronological order, each of the form {"sender": <str>, "text": <str>}. The result of send and get_log is a fresh list of such dictionaries.

An unrecognized command name should raise ValueError.

Constraints

  • len(commands) == len(params), and at most a few thousand commands.
  • bot_id, channel_id, sender, text, keyword, reply are strings.
  • Multiple bots may be registered; the same bot type may be registered more than once with different ids.
  • Single-threaded, in-memory processing only.

Examples

Example 1

Input: simulate_chat(['register_counter', 'send', 'send', 'send', 'get_log'], [['counter-1'], ['general', 'alice', 'hello'], ['general', 'bob', 'again'], ['dm', 'alice', 'private'], ['general']])

Output: [None, [{'sender': 'alice', 'text': 'hello'}, {'sender': 'counter', 'text': 'Message 1: hello'}], [{'sender': 'alice', 'text': 'hello'}, {'sender': 'counter', 'text': 'Message 1: hello'}, {'sender': 'bob', 'text': 'again'}, {'sender': 'counter', 'text': 'Message 2: again'}], [{'sender': 'alice', 'text': 'private'}, {'sender': 'counter', 'text': 'Message 1: private'}], [{'sender': 'alice', 'text': 'hello'}, {'sender': 'counter', 'text': 'Message 1: hello'}, {'sender': 'bob', 'text': 'again'}, {'sender': 'counter', 'text': 'Message 2: again'}]]

Example 2

Input: simulate_chat(['register_keyword', 'send', 'get_log'], [['help-1', 'help', 'How can I help?'], ['support', 'sam', 'I need HELP'], ['support']])

Output: [None, [{'sender': 'sam', 'text': 'I need HELP'}, {'sender': 'keyword', 'text': 'How can I help?'}], [{'sender': 'sam', 'text': 'I need HELP'}, {'sender': 'keyword', 'text': 'How can I help?'}]]

Hints

Hint 1

Separate the engine from the individual bot types: define one common reply contract that takes the message and a state dictionary, so the dispatch loop never inspects bot types.

Hint 2

Keep a per-channel record holding the log and a dictionary mapping bot_id to that bot's state; create it lazily on the first send, but never in get_log.

Hint 3

Remember to append each bot reply right after it is produced, pass the original user message to every bot, and convert log entries to plain dictionaries when returning.