Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

AI Word Filtering Coding

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

Your question is AI Word Filtering Coding. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output