Solve Wonderful Strings and Grid Queries
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithm design and data-structure competencies through two problems: a parity-constrained minimum-cost string transformation over a 10-letter alphabet and threshold-based grid reachability under many queries.
Part 1: Minimum-cost Wonderful String
Constraints
- 0 <= len(s) <= 200000
- s contains only characters from 'a' to 'j'
- cost is a 10 x 10 matrix
- 0 <= cost[i][j] <= 10^9
- cost[i][i] = 0
- The answer may be larger than 32-bit integer range
Examples
Input: ("", [[0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]])
Expected Output: 0
Explanation: The empty string already satisfies the condition.
Input: ("ab", [[0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]])
Expected Output: 1
Explanation: For length 2, all counts must be even. Change either character into the other for cost 1.
Input: ("abcd", [[0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]])
Expected Output: 2
Explanation: The string has four odd counts. Two replacements are enough to pair letters into even counts.
Input: ("aabccd", [[0, 9, 9, 9, 9, 9, 9, 9, 9, 9], [9, 0, 9, 5, 9, 9, 9, 9, 9, 9], [9, 9, 0, 9, 9, 9, 9, 9, 9, 9], [9, 2, 9, 0, 9, 9, 9, 9, 9, 9], [9, 9, 9, 9, 0, 9, 9, 9, 9, 9], [9, 9, 9, 9, 9, 0, 9, 9, 9, 9], [9, 9, 9, 9, 9, 9, 0, 9, 9, 9], [9, 9, 9, 9, 9, 9, 9, 0, 9, 9], [9, 9, 9, 9, 9, 9, 9, 9, 0, 9], [9, 9, 9, 9, 9, 9, 9, 9, 9, 0]])
Expected Output: 2
Explanation: Only 'b' and 'd' have odd counts. Changing 'd' to 'b' costs 2 and makes all counts even.
Hints
- Only the parity of each final letter count matters. A 10-bit mask is enough to represent the odd/even state of all letters.
- Process equal source letters together. If you decide which target letters receive an odd number of copies, every remaining copy can be sent in pairs to the cheapest target for that source letter.
Part 2: Count Reachable Grid Cells for Threshold Queries
Constraints
- 1 <= m * n <= 100000
- 1 <= len(queries) <= 100000
- 1 <= grid[r][c] <= 10^9
- 1 <= queries[i] <= 10^9
Examples
Input: ([[1, 2, 3], [2, 5, 7], [3, 5, 1]], [5, 6, 2])
Expected Output: [5, 8, 1]
Explanation: For q = 5, only cells with values 1, 2, or 3 are allowed and 5 cells are reachable. For q = 6, all except the 7 are reachable.
Input: ([[5]], [1, 5, 6])
Expected Output: [0, 0, 1]
Explanation: The start cell itself must also satisfy the strict inequality. It becomes reachable only when q > 5.
Input: ([[1, 1, 2], [3, 2, 4]], [2, 5, 2])
Expected Output: [2, 6, 2]
Explanation: Sorting queries internally is helpful, but answers must be returned in the original order.
Input: ([[1, 100, 1], [1, 100, 1], [1, 1, 1]], [50, 101])
Expected Output: [7, 9]
Explanation: At threshold 50 the two cells with value 100 are blocked, but the other 7 cells are still connected through the bottom row.
Hints
- If you process queries from small to large, cells reached for a smaller query never need to be processed again for a larger one.
- Use a min-heap frontier starting from (0, 0). Repeatedly expand the smallest-valued reachable boundary cell while its value is below the current query.