As reported: Google L3 onsite, round 4 (India, January 2025), in two parts: first ignoring the houses' colours, then a follow-up handling the colours; a
Houseclass was designed, and the output format was left to the candidate. The same problem was asked in a Google L4 onsite round 1 (November 2023).
Houses are grouped into neighborhoods, given as a 2D array of house numbers: row i is neighborhood i. Rearrange the houses among the neighborhoods so that
- the number of neighborhoods and the size of each stay the same;
- within every neighborhood the house numbers are in ascending order, with no number repeated inside a neighborhood.
Follow-up. Each house also has a colour code, given as a 2D array of the same shape. A house keeps its colour when it moves; return the houses with their colours.
Example
numbers = [[8,2,9], colours = [[r,g,b],
[4,6,4], [w,c,b],
[4,5,1]] [x,y,b]]
numbers only: [[1,2,4], [4,5,6], [4,8,9]] or [[1,4,6], [2,4,8], [4,5,9]] ...
with colours: [[1b,4b,6c], [2g,4x,8r], [4w,5y,9b]] (one valid answer)
Clarifications
- All neighborhoods have the same size.
- Any valid arrangement is accepted. If none exists, return an empty list.
- Houses with the same number may have different colours; each house is moved as a whole.
Constraints: 1 ≤ k neighborhoods of size m, k · m ≤ 10^5; house numbers in [1, 10^9]; colour codes are 1–5 lowercase letters.
Code runner format: the first line is k m, then k lines of m houses written as the number followed by the colour code (8r). Implement arrangeHouses(numbers, colours) returning the neighborhoods as lists of such tokens ("1b"). The harness checks the rules (same houses, same shape, every neighborhood strictly ascending) and prints ok, or impossible for an empty list (or what is wrong).