Validate abbreviations and brackets
Company: Meta
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string parsing, pattern validation, and algorithmic correctness, covering abbreviation interpretation and bracket balancing while requiring careful handling of edge cases and input constraints.
Part 1: Abbreviation Validation
Constraints
- 0 <= len(word) <= 100000
- 0 <= len(abbr) <= 100000
- `word` contains only lowercase English letters
- `abbr` contains only lowercase English letters and digits
- Any numeric part of `abbr` must represent a positive integer with no leading zeros to be valid
Examples
Input: ("internationalization", "i12iz4n")
Expected Output: True
Explanation: `i` matches, `12` skips 12 letters, `i` and `z` match, `4` skips 4 letters, and `n` matches the final character.
Input: ("apple", "a2e")
Expected Output: False
Explanation: After matching `a` and skipping 2 characters, the next character in `word` is `l`, not `e`.
Input: ("", "")
Expected Output: True
Explanation: An empty abbreviation validly represents an empty word.
Input: ("word", "4")
Expected Output: True
Explanation: The number `4` skips the entire word exactly.
Input: ("substitution", "s010n")
Expected Output: False
Explanation: The numeric part starts with `0`, which is not allowed.
Hints
- Use two pointers: one for `word` and one for `abbr`.
- When you see a digit, parse the full number first, then advance the word pointer by that amount.
Part 2: Balanced Bracket Validation
Constraints
- 0 <= len(s) <= 100000
- `s` contains only the characters `(`, `)`, `[`, `]`, `{`, and `}`
Examples
Input: "()[]{}"
Expected Output: True
Explanation: Each bracket is correctly opened and closed.
Input: "([)]"
Expected Output: False
Explanation: The order is wrong: `(` opens first, but `]` tries to close `[` before `)` closes `(`.
Input: ""
Expected Output: True
Explanation: An empty string is valid by definition.
Input: "}"
Expected Output: False
Explanation: A closing bracket appears without any matching opening bracket.
Input: "{[]}"
Expected Output: True
Explanation: Nested brackets are properly matched and closed in order.
Hints
- Use a stack to keep track of the opening brackets you have not matched yet.
- When you see a closing bracket, it must match the most recent unmatched opening bracket.