Solve Stack Decoding and Fuel-Constrained Paths
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This prompt evaluates proficiency in parsing and string manipulation for nested repeated-pattern decoding and in graph search and state-space modeling for fuel-constrained shortest paths.
Part 1: Decode a Repeated-Pattern String
Constraints
- 0 <= len(s) <= 100000
- s contains lowercase English letters, digits, and the characters '[' and ']'
- Repeat counts are positive integers
- The encoded string is valid
- The decoded output length fits in memory
Examples
Input: '3[a]2[bc]'
Expected Output: 'aaabcbc'
Explanation: 'a' is repeated 3 times and 'bc' is repeated 2 times.
Input: '3[a2[c]]'
Expected Output: 'accaccacc'
Explanation: First decode 'a2[c]' into 'acc', then repeat it 3 times.
Input: '2[abc]3[cd]ef'
Expected Output: 'abcabccdcdcdef'
Explanation: Two repeated sections are expanded, then the trailing 'ef' is appended.
Input: '10[a]'
Expected Output: 'aaaaaaaaaa'
Explanation: This checks that multi-digit repeat counts are handled correctly.
Input: ''
Expected Output: ''
Explanation: Edge case: an empty input should produce an empty output.
Hints
- Use one stack for repeat counts and another stack for the string built before each '['.
- If the repeat count has multiple digits, keep building the number instead of treating each digit separately.
Part 2: Find the Shortest Grid Path With Refueling Stations
Constraints
- 1 <= m, n <= 60
- 1 <= fuelCapacity <= 100
- Each grid cell is one of 'S', 'T', '.', '#', or 'G'
- There is exactly one 'S' and exactly one 'T'
- You may revisit the same cell if the remaining fuel is different
Examples
Input: (['S..#', '.G.#', '...T'], 4)
Expected Output: 5
Explanation: A shortest valid route is S -> right -> down to G -> down -> right -> right to T.
Input: (['ST'], 1)
Expected Output: 1
Explanation: Edge case: the target is adjacent to the start.
Input: (['S.T'], 2)
Expected Output: 2
Explanation: A simple straight path with exactly enough fuel.
Input: (['S..', '...', '..T'], 3)
Expected Output: -1
Explanation: The shortest path needs 4 steps, but there is no gas station and the tank holds only 3.
Input: (['S#T'], 5)
Expected Output: -1
Explanation: The wall blocks the only possible path.
Hints
- A normal BFS over only (row, col) is not enough; include remaining fuel in the state.
- After spending 1 fuel to move into a neighbor, reset fuel to fuelCapacity if that neighbor is a gas station.