You are given a simple chat room system.
The system has three global data structures:
aways: Dict[str, str] = {}
messages = []
tacos: Dict[str, int] = {}Implement or explain the function:
def receive(name: str, msg: str):
The function is called every time a user sends a message.
For every received message, the system should first add the original user message to messages in the following format:
"name: msg"
The chat room also supports several bot behaviors.
AwayBot
The system keeps an aways dictionary.
The key is a user name, and the value is that user’s away message.
When any new message mentions a user who is currently away, the system should append an automatic bot response:
"AwayBot: <user> is away: <away_message>"
A user can set themselves as away by sending a message that starts with:
@away
For example:
receive("@alice", "@away having lunch")This should mark @alice as away with the message:
"having lunch"
The system should also append:
"AwayBot: @alice is now away: having lunch"
MeetBot
A user can start a Google Meet by sending a message that starts with:
@meet
For example:
receive("@alice", "@meet @bob")The system should append a bot message similar to:
"MeetBot: Google Meet with @alice, and @bob starting at google meet link"
After creating the meeting, both users should be marked as away.
The sender should get an away message like:
"@alice may be in a meeting right now"
The invited user should get an away message like:
"@bob may be in a meeting right now"
TacosBot
The chat room also supports a taco reward command.
A user can give tacos by sending a message that starts with:
@givetaco
The message format is:
@givetaco <number_of_tacos> <receiver>
For example:
receive("@alice", "@givetaco 3 @bob")The system should add 3 tacos to @bob.
If @bob does not exist in the taco counter yet, initialize their taco count to 0 before adding the new tacos.
Then append a bot message similar to:
"TacosBot: @alice gave @bob 3 🌮s. @bob now has 3 🌮s"
Goal
Given this chat system, implement the receive function so that it correctly:
- records the original user message,
- triggers
AwayBotwhen an away user is mentioned, - handles the
@meetcommand, - handles the
@givetacocommand, - handles the
@awaycommand, - preserves the final message order in
messages.
面试中可以先说整体流程:
每次收到消息,我会先保存原始消息。然后检查当前消息是否提到了 away 用户。接着判断消息是不是 bot command。不同 command 会更新不同的全局状态,并把对应 bot 回复追加到 messages 里。
实现时可以把命令按空格拆开:
parts = msg.split()
这样更容易判断:
parts[0] == "@away"
parts[0] == "@meet"
parts[0] == "@givetaco"
@away 后面的内容可以用空格重新拼起来。@meet 取第二个参数作为被邀请用户。@givetaco 取第二个参数作为数量,第三个参数作为接收人。
边界情况
这题可以简单补充几个边界情况:
如果 command 参数不完整,可以不触发 bot,或者返回错误提示。
如果 taco 数量不是数字,需要跳过或提示格式错误。
如果 mention 检测用 if user in msg,可能会误匹配。例如 @ann 可能匹配到 @anna。更稳的做法是把消息拆成单词后再判断。
总结
这道题的关键是维护三个状态:messages、aways、tacos。
messages 记录输出顺序。
aways 记录用户状态。
tacos 记录累计数量。
整体思路就是:保存原始消息,检查 away mention,解析 command,更新状态,追加 bot 回复。这个结构讲清楚,面试官就能看到候选人对代码逻辑和业务规则的理解。
csoahelp 做的就是这种场景里的实时辅助,题目刚读完就能给出完整思路,现场不用卡壳。
我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

