Google | Onsite | Min steps to reach the state

Given 2x4 matrix it contains numbers in the range 1-8.
For example:

[[4,2,6,5],
 [1,8,7,3]]

You have 3 operations:

  1. Rotate: Middle 4 numbers rotate in clockwise manner.
    [4,2,6,5
    1,8,7,3]
    becomes
    [4,8,2,5
    1,7,6,3]

  2. Swap: swaps the two arrays
    [4,2,6,5
    1,8,7,3]
    becomes
    [1,8,7,3
    4,2,6,5]

  3. Shift (right):
    [4,2,6,5
    1,8,7,3]
    becomes
    [5,4,2,6,
    3,1,8,7]

Your goal is to reach following state and return how many minimum operations would it take to reach that state.

[[1,2,3,4],
 [5,6,7,8]]

Similar questions:

Full interview

Comments (8)