PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates proficiency in string pattern matching, recursive problem decomposition, and algorithmic optimization including handling wildcard semantics and complexity analysis.

  • medium
  • Citadel
  • Coding & Algorithms
  • Data Scientist

Match a string with wildcard pattern recursively

Company: Citadel

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a function that checks whether an input string matches a wildcard pattern. ### Pattern rules - `?` matches **exactly one** character. - `*` matches **any sequence** of characters, including the empty sequence. - All other characters match themselves. ### Input - A string `s` - A pattern string `p` ### Output - Return `true` if `s` matches `p` entirely (not a substring match), otherwise `false`. ### Examples - `s = "aa"`, `p = "a"` → `false` - `s = "aa"`, `p = "*"` → `true` - `s = "cb"`, `p = "?a"` → `false` - `s = "adceb"`, `p = "*a*b"` → `true` ### Constraints (typical interview assumptions) - `0 ≤ len(s), len(p) ≤ 2000` - You should discuss a recursive approach and how to avoid exponential blow-up.

Quick Answer: This question evaluates proficiency in string pattern matching, recursive problem decomposition, and algorithmic optimization including handling wildcard semantics and complexity analysis.

Implement a function that checks whether a string matches a wildcard pattern exactly. Pattern rules: - '?' matches exactly one character. - '*' matches any sequence of characters, including the empty sequence. - Any other character matches itself. The match must cover the entire string, not just a substring. A purely naive recursive solution can become exponentially slow because the same subproblems are recomputed many times. An efficient solution should use recursion with memoization (or equivalent dynamic programming) to avoid this blow-up.

Constraints

  • 0 <= len(s), len(p) <= 2000
  • s and p consist of printable characters, where p may also contain '?' and '*'
  • The solution should avoid exponential recursion by caching repeated states

Examples

Input: ("aa", "a")

Expected Output: False

Explanation: The pattern 'a' matches only one character, so it cannot match the entire string 'aa'.

Input: ("aa", "*")

Expected Output: True

Explanation: The '*' wildcard can match any sequence, including 'aa'.

Input: ("cb", "?a")

Expected Output: False

Explanation: '?' matches 'c', but then 'a' does not match 'b'.

Input: ("adceb", "*a*b")

Expected Output: True

Explanation: The first '*' can match an empty prefix, the second '*' can match 'dce', and the remaining letters align.

Input: ("", "")

Expected Output: True

Explanation: An empty pattern matches an empty string.

Input: ("", "*")

Expected Output: True

Explanation: The '*' wildcard can match the empty sequence.

Input: ("", "?")

Expected Output: False

Explanation: '?' must match exactly one character, but the string is empty.

Input: ("abcde", "a*?e")

Expected Output: True

Explanation: 'a' matches 'a', '*' can match 'bc', '?' matches 'd', and 'e' matches 'e'.

Input: ("abc", "a**c")

Expected Output: True

Explanation: Consecutive '*' characters still behave like a wildcard sequence matcher, so this matches 'abc'.

Input: ("acdcb", "a*c?b")

Expected Output: False

Explanation: No way of expanding '*' makes the rest of the pattern match the entire string.

Hints

  1. Think of each recursive state as a pair of indices: one in the string and one in the pattern.
  2. If you memoize the answer for each (i, j), you can avoid recomputing the same wildcard decisions many times.
Last updated: May 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Sort a Nearly Sorted Array - Citadel (hard)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Design dynamic weighted random sampling with updates - Citadel (medium)
  • Compute maximum later-earlier difference - Citadel (medium)