Archive of articles classified as' "Facebook Hacker Cup 2012"

Back home

This Programming Problem Just Spins My Head Round Round! – Facebook Sample Puzzle

28/08/2012

Hi Readers,

I have not updated even a single thing on my blog, since past so many days, I do not need to make you guess what I was up to. But have been trying to polish my skill set as much as possible.

The other day I came across this problem, from the Facebook Sample Puzzle (unfortunately they are using this puzzle also as a sample for interview questions), the worst part is. The solution is not available anywhere, nor does the sample submitter validates it.

Here is the Problem Statment:

There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves.

  • A move consists of picking the topmost disc of any one of the pegs and placing it on top of anyother peg.
  • At anypoint of time, the decreasing radius property of all the pegs must be maintained.

Constraints:
1<= N<=8
3<= K<=5

Input Format:
N K
2nd line contains N integers.
Each integer in the second line is in the range 1 to where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration.
3rd line denotes the final configuration in a format similar to the initial configuration.
Output Format:
The first line contains M - The minimal number of moves required to complete the transformation. 
The following M lines describe a move, by a peg number to pick from and a peg number to place on.
If there are more than one solutions, it’s sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one.

Sample Input #00:

Sample Output #00:

Sample Input #01:

Sample Output #01:

NOTE: You need to write the full code taking all inputs are from stdin and outputs to stdout 
If you are using “Java”, the classname is “Solution

The first thought for everyone should be Tower of Hanoi. But actually the problem is far more complex, as ToH is a recursive 101 program. What I found most complex in the statement is the line “You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one” which somewhat means that it won’t offer you problems with results more than 6 (should be less than 7). Because lets say the case is as follows:

7 4
2 2 4 3 1 1 4
3 3 3 3 2 2 1

How come can it be in less than 7 moves?.. Please correct me if i am missing something…

My Take:

I just sat up and wrote the code, debugged it like 150 times to solve at least some test cases (Moves ~ 6) . But the case mentioned above is still not solvable by the code.

Again I might be wrong, my code is messy, my algo is just not clean, whosoever understands this better, please guide me and correct me. Here is the working version on IdeOne: http://ideone.com/Le8pw

Cheers!

Incoming search terms:

there are k pegs each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg there are n discs which have radius 1 to n; given the initial configuration of the pegs and the final configuration of the pegs output the mov | songs pk | nishantarora in random-post-box-load | c program coding to find There are K pegs Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration o | there are k pegs each peg can hold discs in decreasing order of radius when looked from bottom to top of the | there are k pegs each peg can hold discs in decreasing order | there are k pegs | sample input 3 1 2 facebook | n k 2nd line contains n integers each integer in the second line is in the range 1 to k where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration 3rd line denotes the final configuration in a format similar t | here are K pegs Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs output the move | here are K pegs Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg | here are k pegs each peg can hold discs in decreasing order | facebook sample puzzle | facebook sample problems | facebook k peg | each integer in the second line is in the range 1 to k | what if i format my hp 431? will the heating problems will be solved |
No Comments