My first instinct was regex and I almost went that route before catching myself.
Clarify the exact pattern syntax and error handling expectations, then propose a single-pass scan using a stack or state machine to identify patterns and replace them. Discuss trade-offs between regex-based and manual parsing, and emphasize handling edge cases like nested or malformed patterns.
Pro tip: Mention that you would confirm whether patterns can be nested or contain percent signs, as this drastically changes the parsing strategy. Also, suggest returning a specific error message format to make debugging easier.
Ask about pattern syntax (e.g., can patterns contain spaces, nested percent signs, or be empty?), error handling (return error string vs throw exception), and expected output format.
Decide between regex (simple but may fail on nested patterns) and manual scanning (more robust). Explain the trade-offs and pick one based on clarified requirements.
Scan the string, identify patterns between percent signs, look them up in the dictionary, and build the result. If a pattern is missing, return the error string immediately.
Address cases like unmatched percent signs, empty patterns, patterns with special characters, and multiple occurrences. Ensure error handling is consistent.
Discuss time and space complexity (O(n) time, O(n) space for output). Walk through test cases including normal, missing pattern, and malformed input.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.