Implement list and grid transformation tasks
Company: eBay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in character classification, array and list manipulation, greedy repartitioning logic, and iterative grid simulation with simultaneous state updates and gravity effects.
Part 1: Uppercase–lowercase difference
Constraints
- 0 <= len(chars) <= 200000
- Each element of `chars` is a string of length 1
- Characters may be letters, digits, punctuation, whitespace, or other symbols
Examples
Input: (['A', 'b', 'C', 'd', '!'])
Expected Output: 0
Explanation: There are 2 uppercase letters and 2 lowercase letters, so the result is 2 - 2 = 0.
Input: ([])
Expected Output: 0
Explanation: An empty list contains no uppercase or lowercase letters.
Input: (['x', 'y', 'Z'])
Expected Output: -1
Explanation: Uppercase: 1 (`Z`), lowercase: 2 (`x`, `y`), so 1 - 2 = -1.
Input: (['1', ' ', '?'])
Expected Output: 0
Explanation: None of the characters are letters, so the score stays 0.
Input: (['M'])
Expected Output: 1
Explanation: A single uppercase letter contributes +1.
Hints
- Scan the list once and keep a running total.
- If a character is uppercase, add 1. If it is lowercase, subtract 1. Ignore everything else.
Part 2: Repartition numbers into two lists, then merge
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- You should process the numbers in left-to-right order
Examples
Input: ([1, 2, 3, 4])
Expected Output: [1, 3, 2, 4]
Explanation: 1 goes to A. 2 goes to B because it is at least both maxima and B is shorter. 3 goes to A on a length tie. 4 goes to B.
Input: ([])
Expected Output: []
Explanation: With no numbers, both lists stay empty.
Input: ([7])
Expected Output: [7]
Explanation: The first element goes to A because the lists are tied in length.
Input: ([2, 1, 2, 1, 2])
Expected Output: [2, 1, 1, 2, 2]
Explanation: This case checks both the larger-maximum rule and the tie-breaking rule when maxima are equal.
Input: ([3, 5, 2, 6, 1])
Expected Output: [3, 6, 1, 5, 2]
Explanation: 2 is appended to B because B has the larger current maximum, while 1 is appended to A because A's maximum becomes larger later.
Hints
- You do not need to recompute each list's maximum from scratch every time; keep the current maximum of `A` and `B` as you build them.
- There are two separate decision rules: one based on the current maxima, and one based on the current lengths.
Part 3: Balloon explosion in an n x n grid with gravity
Constraints
- 0 <= n <= 50
- `grid` is an `n x n` matrix
- 0 <= grid[r][c] <= 10^9
- Only 4-directional neighbors count; diagonal cells do not
Examples
Input: ([])
Expected Output: []
Explanation: An empty grid is already stable.
Input: ([[7]])
Expected Output: [[7]]
Explanation: A single balloon has no neighbors, so it cannot explode.
Input: ([[1, 0, 0], [1, 2, 0], [1, 2, 2]])
Expected Output: [[0, 0, 0], [1, 0, 0], [1, 2, 2]]
Explanation: The middle `1` and the lower-left `2` explode in the first round. Then gravity pulls the remaining balloons down, and no further explosions occur.
Input: ([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
Expected Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Explanation: Every balloon has at least 2 same-valued neighbors, so the whole grid explodes in one round.
Input: ([[3, 3, 0, 2, 0], [3, 3, 0, 0, 2], [0, 0, 0, 2, 0], [0, 0, 0, 0, 2], [0, 0, 0, 0, 0]])
Expected Output: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Explanation: First the 2x2 block of `3`s explodes. Gravity then forms a 2x2 block of `2`s in the lower-right corner, which explodes in the next round.
Hints
- In each round, first collect all cells that should explode based on the grid at the start of that round. Do not update cells one by one during the scan.
- For gravity, process each column from bottom to top and write non-zero values downward to keep the fall stable.