Tower of Hanoi Problem Statement

You have three rods numbered from 1 to 3, and 'N' disks initially stacked on the first rod in increasing order of their sizes (largest disk at the bottom). Your task is to move the 'N' disks to another rod (either rod 2 or rod 3) by following the rules below in less than 2N moves.

1. Only one disk can be moved at a time. 
2. Larger disks cannot be placed on top of smaller disks. 
3. Disks can only be moved from the top of a rod.
Note:
The initial size of the 'i'th disk from the top of the stack is equal to 'i', meaning the bottom disk has size 'N', the disk above it has size 'N-1', and so forth, with the top disk being size 1.

Example:

Example

Input:

The first line contains an integer 'T', the number of test cases. Each test case contains a single integer 'N', representing the number of disks.

Output:

For each test case, output a 2-D array/list where each row contains two integers. The first integer is the number of the rod from which the topmost disk is moved, and the second integer is the rod number to which the disk is placed. The result is '1' if all disks are successfully moved to rod 2 or 3, otherwise, '0'.
Note:
Printing is not required; simply implement the function to return the answer.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 12
  • 'T' represents the number of test cases, 'N' represents the number of disks.
  • Time Limit: 1 second
Be the first one to answer
Select
Add answer anonymously...

Top Full Stack Developer Interview Questions Asked at MakeMyTrip

Q. Make Array Elements Equal Problem Statement Given an integer array, your objecti...read more
Q. Minimum Operations Problem Statement You are given an array 'ARR' of size 'N' co...read more
Q. Square Root with Decimal Precision Problem Statement You are provided with two i...read more
Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits