Google Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
3
of 3 vote

If anyone understands this question, could you elaborate, a bit? I'd def have to ask my interviewer for more info.

- Anonymous December 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya

- Anonymous January 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

no, the best way is to use quicksort, how? imagine the empty slot as pivot.

- geniusxsy November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain a little more? All elements less than the pivot should be on the left and more than the pivot on the right; how could we use the empty place as the pivot?

Also, are we assuming the cars in the final order are sorted?

- Anonymous November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think the cars in the final order are sorted. The final order may be 6 5 4 3_ 2 1, ect. The problem does not mean the final order have to be in sorted order

- Anonymous November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do I explain this to you?
think the following:
car: a, b, c, d, e
new order is: b, e, d, c, a

it equivalent to:
sort 5, 1, 4, 3, 2 to 1,2,3,4,5

get the idea?

- geniusxsy November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You really are one kind of a dumbass genius. Just because the problem can solved by sorting does not mean you have to use it. Use your brain and think of a better way.

- Anonymous January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Okay i think it is 1 2 3 4 5 6 is similar to 1 2 3 4 5 _ 6,,,

so variant of insertion sort or bubble sort can fix this problem,, but this is just one look to the problem,, can someone give a more efficient method what if there are 100,000 such cars

- chhetriarun84 November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(If assume that the final order is sorted?)
1. We could shift the _ (empty place) at the end.
2. Use quicksort on the rest of that array. We use the empty slot at the end to move the pivot in the correct position. Note, that after putting the pivot in the correct position, we again see to that the _ (empty place) is at the end.
3. When the array is sorted we move the _ (empty place) back to where ever it belongs in the final position.
(nlgn solution)


(If assume that the final order could be anything?)
1. Go through first array and save key value pairs (key = value at index i ; value = i) in a hashtable.
2. Now lets start arranging the cars in thier position according to array 2;
for each element in array 2 (when we encounter a _ ignore it, only numbers),
find its orginal position using the hash table, also find the the the position of the empty place. Using these positions see to that the correct car is in position i in the first array. Note that this change would update some values in the hash table too (like new position of the empty place).
At the end the empty place will be in the position we want it to be. (I hope this method makes sense!).
(n solution)

- Anonymous November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I solved the problem assuming exactly the second condition,and taking the approach exactly same as yours.The time complexity is obviously O(n),but we have O(n) space overhead.

- random November 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a little more explanation on projecting this problem to quick sort:
1. observe that: if you maintain the order of cars, shifting the empty slot from one place to any other place takes O(n)
2. in this problem, you always swap a car with the empty slot. Similarly, in quicksort, the swap operation always involves the pivot element;

- geniusxsy November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shoudln't shifting the empty slot take O(1) time? As in,
1 2 3 4 5 _ to _ 2 3 4 5 1 is 0(1)? Would'nt taking car 1 out and parking it in in the last place be just one operation?

- Anonymous November 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No you fool, you are allowed to move the _ to non-adjacent locations. WTF.

- Anonymous January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So, as the question says,, isn't it just a swapping problem

- Arun Chhetri November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Give the '_' slot a value between the element below and above it in the proposed sort order. Then sort using a sort algorithm (depending on size of array, different sorting algorithms would be used).

- anon November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you didn't get the point.
the point is, any swap operation is restricted to have the empty slot as one of the two operands.
consequently, quick sort is the best candidate.

- geniusxsy November 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

genius you are the one not getting the point. I suggest changing your username. Stupid pedantic fool.

- Anonymous January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming we are given the initial order(I) and the final order(F).
Our algorithm should formulate the steps to transform I to F.

reorder(I, F, n, p) { // I = intial order, F = final, n = no. of elements, p = pivot
  for(i = 1; i <= n; i++) {
    if(I[i] = F[i]) continue;
    j = find(I, F[i]); //find the index of element F[i] in the array I.
    swap(I, i, pivot); // swap the elements I[i], I[pivot]
    swap(I, j, i);
    pivot = j;
  }
}

- Thiyaneshwaran S November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2314_56->2_14356->_214356->12_4356->1234_56->12346_6

- Anonymous November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2314_56->2_14356->_214356->12_4356->1234_56->12345_6

- Anonymous November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Thiyaneshwaran

How do u implement 'find'?
It may require O(N) time without 'hashes'.
The total running time would be O(N^2).

- Hari November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Thiyaneshwaran

How do u implement 'find'?
It may require O(N) time without 'hashes'.
The total running time would be O(N^2).

- Hari November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we consider only the swaps, (which is the crucial part here), the alg looks fine...

- Hari November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explanin your algorithm in detail?

- Venu June 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why a greedy approach works here? Any proofs? (I think we are trying to find the minimum order, isn't it?)

- Altruist November 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Hari
Yes, the time complexity is O(n2) in the above algorithm. Surely there will be a better approach.

- Thiyanesh November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same approach.. Java code here. Complexity is again n^2
public static int[] rearrangeParking(int exist_order[],int new_order[] ) throws Exception
{
int blankSlot = -1;

for(int i=0;i<exist_order.length;i++)
{
if(exist_order[i] == 0)
{
blankSlot = i;
break;
}
}


int carFoundAt = -1;
for(int i=0;i<exist_order.length;i++)
{
//expected is not same as original
if(exist_order[i]!=new_order[i])
{

//use blank space
exist_order[blankSlot] = exist_order[i];//park this car in the blank slot
exist_order[i] = 0; // current slot is blank now
blankSlot = i;


//find the new_order[i] car in the current parking lot
carFoundAt =-1;
for(int j=0;j<exist_order.length;j++)
{
if(exist_order[j] == new_order[i])
{
carFoundAt = j;
break;
}
}

if(carFoundAt!=-1)// this car found at the parking slot
{
//take car to its new position

exist_order[blankSlot] = exist_order[carFoundAt];//park the car in the blank slot
blankSlot = carFoundAt;
exist_order[blankSlot]=0;


}
else
{
throw new Exception("Specificed order is incorrect, car:"+new_order[i]+" not found");
}

}


}


return exist_order;

}

- Ganesh November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tried this, works fine with all cases.. Except the fact that the complexity is high. Any comments?

- Ganesh November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@geniusxsy

Please provide the quicksort-style pseudocode. In the CLR quicksort implementation (ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/LectureNotes/lec4.pdf), the pivot is *not* always one of the swap operands:

int partition(int[] a, int low, int hi){
    int pivot = a[low];
    int i = low;
    for (int j = low+1; j <= hi; j++) {
       if (a[j] <= pivot){
           i++;
           swap(a[i],a[j]);
    }
    swap(a[i],pivot);
    return i;
}

O(n) with O(n) space complexity seems like the best we can do.

- ellemeno December 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Err... What?

- Anonymous December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pretty easy. Think as qsort.

- T December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No sorting needed folks. Firstly, move the _ to the correct position by having the car that is presently there move to current _. Using the given example, this means:
1 2 3 4 5 _ 6

2 3 1 4 _ 5 6 ->
2 3 1 4 5 _ 6

Now that _ is in the right place, forget about it:

2 3 1 4 5 6

Go through the lots one by one. If a car is in the wrong place, swap out the wrong car and replace with the right one. After going through the lots, you should of course have the right order, after an O(n) number of swaps.

But how to do swaps? Say you have cars X and Y. Move X to _, move Y to X's old position, then move X to Y's old position. O(1) swap. So O(n) time for the whole deal.

- Bullocks January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh and O(1) space of course.

- Bullocks January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad ... it can't be O(1) because we need a hashtable to locate the right car.

- Bullocks January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i.e. O(n) space. But time still O(n)

- Bullocks January 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i do have to say although i agree with most of what you said, i have to say that because a swap between a car and an empty space is ideal, if we ever get to a point where the empty space in initial config and final config are at the same place, we take a random car that is not in the right position to swap with the empty spot. Then we start the algorithm up accordingly.

So for example, if final config if [1 2 3 4 _] and starting config is [3 1 2 _ 4]:

3 1 2 _ 4 -> 3 1 2 4 _ -> (now we have the empty spaces aligned, which is a problem. but don't start swapping between two cars since this will take 3 steps per swap. swap a random car (#3 let's say) that is not in position with the empty space) -> _ 1 2 4 3 -> (then proceed as usual) 1 _ 2 4 3 -> 1 2 _ 4 3 -> 1 2 3 4 _

- bk September 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I will try to present my understanding..I Could be wrong...
suppose there are 5 slots in both p1 and p2. 4 cars with the same Id come to p1 and 4 come to p2. They might occupy different spots in their respective lots. we need to rearrange the cars in a particular lot(p1 or p2)so that they occupy the same spot in the both p1 and p2.

If this is the question i don't think the solution is very tough..But i doubt that this is the question..

- Musheka January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you take 5 slots in total then total cars are 4. So 4 do not go to A and 4 to B. (not 8 cars in total).

- cirus February 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keys is to find the most efficient way to do it. This is a variation of the edit distance problem with the cars looking ABCD_ in the case of P1 and say BDAC_ in the case of P2.

- saraks.kumar January 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone state the question properly once again...what exactly does the interviewer mean when he asks "Design an algorithm to let n-1 cars in P1 and P2 park in the same slots"

- Anonymous March 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yea, what exactly is this question about?

- Anonymous March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i don't understand the question :(

- isitjustme April 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

improve your English, man!
it took me 20+ minutes to understand what this problem is talking about...

you should have said that the cars in P1 and P2 have same range of ID but have unclustered allocation in both parking lots.

- beyondfalcon April 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

en.wikipedia.org/wiki/File:Quicksort-diagram.svg

- beyondfalcon April 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do not think we need to sort here. Also, we need not assume that there is car parked in already

- QHu September 08, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More