Solve Stack and String Shift Problems
Company: Bytedance
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This two-part question evaluates proficiency with core data structures and string/array algorithms—specifically stack-based delimiter matching and cumulative character shifting using modular arithmetic—and is commonly asked to assess correctness under ordering constraints, boundary handling, and efficiency.
Part 1: Validate Delimiter Pairs
Constraints
- 0 <= len(s) <= 100000
- s contains only the characters '(', ')', '{', '}', '[' and ']'
- An empty string is considered valid
Examples
Input: "()[]{}"
Expected Output: True
Explanation: Each bracket is correctly matched and closed in order.
Input: "(]"
Expected Output: False
Explanation: The closing bracket ']' does not match the opening bracket '('.
Input: "([{}])"
Expected Output: True
Explanation: Nested brackets are all matched in the correct order.
Input: ""
Expected Output: True
Explanation: Edge case: an empty string has no mismatched brackets.
Input: "]"
Expected Output: False
Explanation: Edge case: a closing bracket appears before any opening bracket.
Hints
- Use a stack to keep track of opening brackets you have not matched yet.
- When you see a closing bracket, it must match the most recent unmatched opening bracket.
Part 2: Apply Cumulative Letter Shifts
Constraints
- 0 <= len(s) <= 200000
- len(shifts) == len(s)
- s contains only lowercase English letters
- 0 <= shifts[i] <= 1000000000
Examples
Input: ("abc", [3, 5, 9])
Expected Output: "rpl"
Explanation: This is the example case. The cumulative effect on the characters produces 'rpl'.
Input: ("z", [1])
Expected Output: "a"
Explanation: Edge case: shifting 'z' by 1 wraps around to 'a'.
Input: ("abc", [0, 0, 0])
Expected Output: "abc"
Explanation: No shifts are applied, so the string stays the same.
Input: ("", [])
Expected Output: ""
Explanation: Edge case: empty input produces an empty output.
Input: ("aaa", [1, 2, 3])
Expected Output: "gfd"
Explanation: The total shifts received by positions 0, 1, and 2 are 6, 5, and 3 respectively.
Hints
- A shift at index `i` affects every character from `0` to `i`, so think about how many total shifts each character receives.
- Process from right to left and keep a running suffix sum modulo 26.