Interview Question


Country: United States




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

For monster A and B to swap places it will take 3 jumps.
First A will jump to empty slot
Then B will jump to vacated A space and then A will jump from empty slot to B
Therefore I think min jumps will be 30 ?
Please correct me if I am wrong.

- DashDash March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The minimum no of moves will be 2*pow(n,2) + 3*n. Where n is the no of monsters.

The sequence for AAAA_BBBB is: (_ is the empty space)
AAA_ABBBB
AAABA_BBB
AAABAB_BB
AAAB_BABB
AA_BABABB
A_ABABABB
ABA_ABABB
ABABA_ABB
ABABABA_B
ABABABAB_
A_ABABAB_
ABA_ABAB_
A_A_ABAB_
A_ABA_AB_
ABA_A_AB_
A_A_A_AB_
A_A_ABA__
A_ABA_A__
ABA_A_A__
A_A_A_A__
A_A_A__A_
A_A_A___A
A_A_A____
A_A__A___
A_A___A__
A_A____A_
A_A_____A
A_A______
A__A_____
A___A____
A____A___
A_____A__
A______A_
A_______A
A________
_A_______
__A______
___A_____
____A____
_____A___
______A__
_______A_
________A

- kathireson March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

AAAAA*BBBBB -> BBBBB*AAAAA
Use two different steps:
a) A moves to neighboring empty slot
b) B jumps over As to empty slot
Using these two steps, it takes N ( where N is the number of monsters of one type ) plus 1 steps to move one B to its final place:
AAAA*ABBBBB (step a)
AAA*AABBBBB (step a)
AA*AAABBBBB (step a)
A*AAAABBBBB (step a)
*AAAAABBBBB (step a)
BAAAAA*BBBB (step b)
You have to do this process for each B, so it is N*(N+1) steps. Finally you have to move each A to its final position using step a. Overall the whole thing will take N*(N+2) steps.

- Peter March 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The total # of turns required is 3 * (Number of monsters on a single side)

For 1 monster each side: total turns = 3
For 2 monsters each side: total turns = 6
For 3 monsters each side: total turns = 9

and so on..

- Sabs April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please explain the question again. Both A and B can jump over the other type. What does it mean.

- DashDash March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have edited the question.

- J.Jolam March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

21
Firstly jump one monster to middle ,then we have blank space on one side jump one monster from other side then we got blank space on other side ,similarly follow this hence we always will get steps=2*(number of monsters) + 1

- rdx March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually, steps= total number of monsters +1

- rdx March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

AAAA*BBBB
AAA*ABBBB
AAABA*BBB
AAAB*ABBB
AAABBA*BB
AAABBAB*B
AAABB*BAB
AAABBB*AB
AAABBB*AB
AAABBBBA*

It seems that there is only one monster can get through the road. The number of steps is 2 * n(monsters) + 1.

The Question did asked how to let "them" get through.
Have I missed something important in the question?

- J.Jolam March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but here again If we go via your method we will need more then 12 steps to complete the solution.

- sonesh March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

n + n/2;where n is total number of monster. here i am assuming 10 A monster and 10 B monster. so h is 20
for example :
6(n) steps from here to
AAA*BBB
AA*ABBB
AABA*BB
A*BAABB
ABBAA*B
*BBAAAB
BBBAAA*
here
now 3(n/2) steps from here to
BBBAA*A
BBBA*AA
BBB*AAA
here
so total is 3*n/2;

- sonesh March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and here there is no backward motion, as A is moving forward only, and B is also moving forward only.

- sonesh March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

BBBAAA* --> BBB*AAA in one step....first A can jump to the last position....

- Anonymous April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the answer is n(n+2),n is the number of monster A or B, which is the same. T he steps begin with A move B jump, then B move A jump 2times ....till A move B jump n times.The line will be like BABABA...BA*,then A move,B jump n-1 times...till A move B jump 1 time. The line will be BBB...BA*A...AA. Then A move. The step number is (1+1)+...(1+n-1)+(1+n)+(1+n-1)+...(1+1)+1=n(n+2).

- FrankLe March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"The line will be like BABABA...BA*,then A move,B jump n-1 times" — how does B jump n - 1 times? if A moves, the lines will become BABABA...B*A. I don’t see how B can jump n - 1 times after A moves. Could you explain?

- YL September 02, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is n * n * 2.

To make each of the monsters on the left go across, it must swap with the n monsters in the right. So, totally, we need n * n swaps.

Each swap needs 2 moves, like the follow.
A*B->*AB->B*A

So, the solution is n * n * 2.

- cat nerd February 16, 2014 | 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