PPractick

DSA interview question

Wildcard Pattern Matching

Hard · Algorithms · Topics: dynamic programming, string, pattern matching

The problem

You're building a file search utility for an internal dev tool, similar to shell globbing (ls *.txt, find file?.log). Given an input string s and a pattern p containing wildcard characters ? (matches any single character) and * (matches any sequence of characters, including the empty sequence), implement a function that returns whether s matches p entirely (not just a substring match).

Function signature

bool isMatch(string s, string p);

Test cases

Input sInput pOutputExplanation
"aa""a"falsep is too short to cover all of s
"aa""*"true* matches the entire string, including everything
"cb""?a"false? matches 'c', but 'a' doesn't match 'b'
"adceb""*a*b*"true* matches empty before a, dce between a and b, empty after b
"acdcb""a*c?b"falseNo valid alignment satisfies the pattern
""""trueBoth empty — trivially matches
"""*"true* can match the empty sequence
"""?"false? requires exactly one character, but s is empty
"mississippi""m??*ss*?i*pi"falseClassic tricky LeetCode edge case — verify with code

(Always double check patterns with consecutive *s or trailing *s against empty suffixes — common source of off-by-one bugs.)

How interviewers phrase it

Clarifying questions to ask

  1. Does * match an empty sequence too, or must it match at least one character?
  2. Does the pattern need to match the entire string, or just find a matching substring?
  3. Can s or p be empty? What should happen in those cases?
  4. Is this the shell-glob style (* = any sequence, ? = any single char), or the regex style (* = zero-or-more of preceding char)? — explicitly confirm, since this is the #1 point of confusion with LeetCode 10.
  5. What's the expected size of s and p? (Signals whether O(n·m) DP is acceptable or if the O(n+m) greedy two-pointer approach is expected.)

Follow-ups to expect

  1. "Can you do this with O(1) or O(min(n,m)) space instead of O(n·m)?"
  2. "How is this different from Regular Expression Matching (LeetCode 10)?"
  3. "Can you solve it with a greedy approach instead of DP?"
  4. "What if consecutive *s appear in the pattern?"
  5. "Can you prove the greedy approach's correctness/termination?"
  6. "What's the worst-case time complexity of the greedy approach?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in