Reconstruct original array from doubled shuffle
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array manipulation, multiset/frequency reasoning, pairing logic, and careful handling of edge cases such as zeros and negative numbers.
Constraints
- 0 <= len(changed) <= 100000
- If len(changed) is odd, the answer is impossible
- Each value in changed can be negative, zero, or positive
Examples
Input: ([2, 1, 4, 8],)
Expected Output: [1, 4]
Explanation: `[1, 4]` produces doubled values `[2, 8]`, and after shuffling we can get `[2, 1, 4, 8]`.
Input: ([-4, -2, -2, -1, 1, 2],)
Expected Output: [-2, -1, 1]
Explanation: `original = [-2, -1, 1]` has doubled values `[-4, -2, 2]`, which together match the multiset in `changed`.
Input: ([0, 0, 0, 0],)
Expected Output: [0, 0]
Explanation: Zero doubles to zero, so four zeroes in `changed` correspond to two zeroes in `original`.
Input: ([1, 2, 4],)
Expected Output: []
Explanation: The length is odd, so `changed` cannot be split into an original half and a doubled half.
Input: ([-6, -3, -3, -12],)
Expected Output: []
Explanation: There are two copies of `-3`, so we would need two copies of `-6` to match them, but only one exists.
Input: ([],)
Expected Output: []
Explanation: An empty `changed` array comes from an empty `original` array.
Hints
- Because the array was shuffled, positions do not matter. Count how many times each value appears.
- Process values in order of increasing absolute value. For each `x`, every remaining copy of `x` must be matched with a copy of `2 * x`. Handle `0` carefully.