Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
AI Word Filtering Coding
00:00
5 left

AI Word Filtering Coding

MediumPython

Problem

Meta AI receives candidate words for autocomplete suggestions. Given a list of lowercase words and a wildcard pattern, return every word that matches the entire pattern, preserving the original order and duplicates.

The pattern contains lowercase letters, ?, and *. A ? matches exactly one lowercase letter. A * matches zero or more lowercase letters. Matching must cover the complete word, not just a substring.

Formal Specification

Implement filter_words(words, pattern):

  • Input: words, a list of lowercase strings, and pattern, a lowercase string containing letters, ?, and *.
  • Output: A list containing each word whose full contents match pattern, in the same order as words.

Example 1:

Input: words = ["chat", "chart", "chatter", "coat"], pattern = "ch*t"
Output: ["chat", "chart", "chatter"]

* absorbs zero characters in chat, one character in chart, and several characters in chatter. coat does not begin with ch.

Example 2:

Input: words = ["cat", "cut", "coat", "cart"], pattern = "c?t"
Output: ["cat", "cut"]

The ? matches exactly one character, so coat and cart are too long.

Constraints

  • 1 <= len(words) <= 10^4
  • 0 <= len(pattern) <= 100
  • 0 <= len(word) <= 100
  • The total number of characters across all words is at most 10^5
  • All word characters are lowercase English letters
  • The pattern contains lowercase English letters, '?' or '*' only

Function Signature

def filter_words(words, pattern):
Interviewer

Your question is AI Word Filtering Coding. Start with the requirements in the Question tab.

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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.