Solve Two Coding Interview Problems
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in parsing and expression evaluation, recursion/stack management, and string-processing for nested function-style arithmetic, alongside graph modeling, geometric distance reasoning, and traversal algorithms for the bomb chain-reaction scenario; the category is Coding & Algorithms.
Part 1: Evaluate Nested Math Expressions
Constraints
- 1 <= len(expression) <= 100000
- The input expression is syntactically valid
- Integers may be negative and may contain multiple digits
- All intermediate and final results fit in a signed 64-bit integer
Examples
Input: "add(1, sub(1, 0))"
Expected Output:
Explanation: sub(1, 0) = 1, then add(1, 1) = 2.
Input: "sub(add(3, 4), sub(10, 6))"
Expected Output:
Explanation: add(3, 4) = 7 and sub(10, 6) = 4, so 7 - 4 = 3.
Input: "sub(-5, add(-2, 3))"
Expected Output:
Explanation: add(-2, 3) = 1, then -5 - 1 = -6.
Input: " -15 "
Expected Output:
Explanation: Edge case: the entire expression is just a single negative integer with surrounding whitespace.
Hints
- Think of the expression as a simple grammar: an expression is either an integer or an operator followed by two expressions inside parentheses.
- Use a shared index while parsing so each character is processed only once.
Part 2: Compute Maximum Chain Reaction
Constraints
- 0 <= len(bombs) <= 100
- -10^9 <= x, y <= 10^9
- 0 <= r <= 10^9
- Use integer arithmetic when comparing distances to avoid floating-point precision issues
Examples
Input: [[0, 0, 2], [2, 0, 2], [5, 0, 1]]
Expected Output:
Explanation: Bomb 0 can trigger bomb 1, but bomb 2 is too far away from both.
Input: [[0, 0, 4], [3, 0, 3], [6, 0, 3], [9, 0, 1]]
Expected Output:
Explanation: Starting from bomb 0 or bomb 1 can trigger the entire chain of four bombs.
Input: []
Expected Output:
Explanation: Edge case: no bombs means no explosions.
Input: [[1, 1, 0]]
Expected Output:
Explanation: Edge case: a single bomb always counts as one exploded bomb.
Input: [[0, 0, 0], [0, 0, 0]]
Expected Output:
Explanation: Even with zero radius, bombs at the exact same position can trigger each other because the distance is 0.
Hints
- Treat each bomb as a node in a directed graph, with an edge from i to j if i can directly trigger j.
- Compare squared distances with squared radii so you never need to use square roots.