Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Messaging Bot Method

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Messaging Bot Method. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Implement a method for a Brigit messaging bot that selects a preset response for each incoming message. A preset matches when its trigger phrase appears as a contiguous sequence of words in the message, ignoring capitalization and punctuation.

If multiple presets match, return the response for the trigger containing the most words. If multiple matches have the same length, use the preset that appears first in presets. If no preset matches, return fallback.

Formal Specification

Implement match_bot_responses(messages, presets, fallback). messages is a list of strings. presets is an ordered list of dictionaries, each containing a nonempty string trigger and a string response. Return a list of response strings in the same order as messages.

Tokenization is defined as extracting contiguous sequences matching [A-Za-z0-9']+, converted to lowercase. For example, "Where's my money?" becomes ["where's", "my", "money"].

Constraints

  • 1 <= len(messages) <= 10^3
  • 1 <= len(presets) <= 10^3
  • Each message contains at most 10^3 normalized words
  • Each trigger contains at most 20 normalized words
  • Triggers are nonempty
  • Responses may be empty strings

Function Signature

def match_bot_responses(messages, presets, fallback):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output