Wildcard Matching

Algorithms
Hard
Adobe
92.3K views

Given an input string $s$ and a pattern $p$, implement wildcard matching with support for '?' (matches any single character) and '*' (matches any sequence). Use DP.

Why Interviewers Ask This

Adobe asks this to evaluate a candidate's mastery of dynamic programming and recursive thinking under pressure. The problem tests the ability to handle state transitions with multiple wildcard rules, specifically managing the non-deterministic nature of the asterisk character. It reveals whether you can optimize naive recursion into an efficient solution while maintaining code clarity.

How to Answer This Question

1. Clarify edge cases immediately, such as empty strings or patterns containing only asterisks, which Adobe often uses to test robustness. 2. Propose a brute-force recursive solution first to establish a baseline understanding of the matching logic for '?' and '*'. 3. Identify overlapping subproblems in the recursion tree to justify the need for Dynamic Programming. 4. Define the DP state explicitly, typically a 2D boolean array where dp[i][j] represents if s[0...i-1] matches p[0...j-1]. 5. Derive the recurrence relation carefully: handle '*' by considering both skipping it and using it to match one or more characters. 6. Optimize space complexity if possible, but prioritize correctness first. 7. Walk through a concrete example like 'aa' and 'a*' to validate your logic before coding.

Key Points to Cover

  • Explicitly defining the DP state transition for the '*' wildcard
  • Correctly initializing the first row to handle leading asterisks
  • Demonstrating understanding of why recursion leads to exponential time without memoization
  • Handling edge cases like empty input strings gracefully
  • Articulating the difference between greedy approaches and DP for this specific problem

Sample Answer

To solve this Wildcard Matching problem efficiently, I would start by analyzing the constraints. Since we have two variable-length inputs, a brute-force approach checking every combination is too slow. Instead, I will us…

Common Mistakes to Avoid

  • Attempting a greedy solution that fails when a '*' could match zero characters versus many
  • Forgetting to initialize the DP table correctly for patterns starting with asterisks
  • Confusing the '*' behavior with regex quantifiers like '+' or '{n}' without clarifying flexibility
  • Overlooking the space optimization opportunity or failing to explain the O(m*n) complexity clearly

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 25 Adobe questions