Google | SDE2 | Phone Screen
Anonymous User
4028

you are given an array of houses in a neighboorhood in a city.
you have to rearrange houses in such a way that in a single neighbourhood the houses are sorted by number in ascending order and no 2 houses with same number are in same neighbourhood.
you can only rearrange house based on the capacity of each neighbourhood . If neighbourhood "1" in input has 2 houses then at output also it can only have 2 houses.

For example-
{
{1,2},
{4,4,7,8},
{4,9,9,9}
}

becomes
{
{4,9},
{1,2,4,9},
{4,7,8,9}
}

can someone suggest what should be the optimal solution for this.
Thanks in advance.

My Solution was :
1)Use a hashmap in java to get the frequency count of each house number.
2)use a priorityqueue to store the houses
-based on there frequency
-if frequency is same then smaller number house should be placed before
In above example 4 number house has 3 frequency and 9 number house also has 3 frequency
so we will first pick 4 and allocate it to all the neighbourhood then do the same for others.
At the end sort the neighbourhood array once again.
I faced rejection in the interview .
I was able to code the entire solution .
I was given 35 minutes for this question .
Can someone think of another approach ?

Comments (23)