Design a Rubik’s cube


  • 0
    S

    Design a Rubik’s cube. Make sure to include a rotate method as well.


  • 0
    L

    @sueee
    I would go with 6 3x3 arrays. However, use 2 circular linked lists. If you were to lay out the cube on a 2d surface it would look like a cross.
    __1
    5_2_6
    __3
    __4

    The first circular linked list will have 1,2,3,4 arrays connected. The second will have 5,2,6,4. From there you have to get the orientation, correct. Say for the sake of the example above, top/bottom represent columns and left/right are rows. If you want to rotate the columns, you would do the operation swap on the first circular linked list and if rows on the second one.


  • 0
    D

    @luce What about size ways rotation? 1-6-5-3


  • 0

  • 0
    L

    Actually after thinking about it, I think the best method is to create each 3x3 array like a node. Each node will have a top, bottom, left and right child. Once all nodes are connected with each other. When you rotate, its a simple iterative cycle swap. That way, you can reuse the same four functions to rotate base on reference node and direction.

    So each node has 4 children, top, bottom, left, right. Each node will also have a 3x3 array. From there have four rotation swap methods. Lastly, an initialization method to connect and create all the nodes properly.

    I still believe you need to clarify with the interview how the rotate will figure out which nodes to rotate and in which direction.


  • 0
    D

    @luce I think, this logic is still flawed as what about the nodes in the corner?


  • 0
    L

    @devyash
    You have 6 nodes, I am not representing each square as a node, I am representing each 3x3 array as a node. Still sort of doing the circular linked list idea previously stated but this will cover the other edge cases without making the code so complex.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.