← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Google SWE coding round, one algorithmic problem the whole session. The problem looked straightforward at first glance but the greedy pairing logic tripped me up a bit before I got it sorted.

Questions Asked (1)

Q1

Given an integer array of even length that was constructed by doubling every element of some original array and shuffling all the values together, recover any valid original array.

Algorithms & Data Structures
Author's notes

I knew the general shape of the solution pretty quickly: sort by absolute value, use a counter, greedily match each element x with 2x and collect x as part of the answer.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the original array elements are exactly half of the doubled values, so we need to pair each element with its double. Use a hash map to count frequencies, then for each element in sorted order, if its count is positive, pair it with its double, decrement counts, and add the element to the result. This greedy approach works because sorting ensures we process smaller elements first, avoiding conflicts.

Pro tip: Clarify that the original array can be in any order, so returning any valid permutation is acceptable. Also, mention that if the problem guarantees a solution, we don't need to handle invalid cases, but we can discuss how to detect them.

1. Understand the problem and constraints

Restate the problem: given an array of even length where each element is double some original element, recover any valid original array. Note that the original array can be in any order, and the input is guaranteed to have at least one valid original array.

2. Choose a data structure for frequency counting

Use a hash map (dictionary) to count the occurrences of each number in the input array. This allows O(1) lookups and updates when pairing elements.

3. Sort the array to process elements in ascending order

Sorting ensures that when we process a number, its double (if present) will be larger, so we won't accidentally use a number that should be paired with a smaller number. This greedy strategy is optimal.

4. Iterate and pair elements

For each number in sorted order, if its count is positive, check if its double exists with positive count. If so, decrement both counts, add the number to the result, and continue. If not, the input is invalid (though guaranteed valid).

5. Return the result and analyze complexity

After processing all elements, return the list of original numbers. Discuss time complexity: O(n log n) due to sorting, and space complexity: O(n) for the hash map and result.

Key Points to Mention

  • Hash map for frequency counting to efficiently track available elements.
  • Sorting to ensure greedy pairing works correctly by processing smaller elements first.
  • Handling duplicates: when multiple copies exist, decrement counts appropriately.
  • Edge cases: zeros (0 doubled is 0, so pairs of zeros), negative numbers (doubling makes them more negative, so sorting still works).
  • Time and space complexity analysis: O(n log n) time, O(n) space.
  • Guarantee of a valid solution means we don't need to handle invalid inputs, but we can discuss how to detect them.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.