DSA interview question
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).
bool isMatch(string s, string p);
Input s | Input p | Output | Explanation |
|---|---|---|---|
"aa" | "a" | false | p 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" | false | No valid alignment satisfies the pattern |
"" | "" | true | Both empty — trivially matches |
"" | "*" | true | * can match the empty sequence |
"" | "?" | false | ? requires exactly one character, but s is empty |
"mississippi" | "m??*ss*?i*pi" | false | Classic 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.)
? and *, similar to shell-style globbing."* here means "any sequence," not "zero or more of the preceding character."* match an empty sequence too, or must it match at least one character?s or p be empty? What should happen in those cases?* = 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.s and p? (Signals whether O(n·m) DP is acceptable or if the O(n+m) greedy two-pointer approach is expected.)*s appear in the pattern?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in