FastPrepTower of Hanoi: Return the Moves
Problems/Tower of Hanoi: Return the Moves

Tower of Hanoi: Return the Moves

JP Morgan Chase logoJP Morgan Chase● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a JP Morgan Chase intern onsite interview. The author reports being asked for recursive and iterative Tower of Hanoi code. We chose the peg labels, the format of the returned moves, the limits, the examples and the rule for zero disks. Our version is about 95% the same as the reported task, including both coding forms.

Move n disks from peg A to peg C using peg B as temporary storage. Move one top disk at a time, and never place a larger disk on a smaller one.

Return a shortest legal sequence of moves. Write each move as "A->C", replacing the two labels with its source and destination. For this three-peg setup, the minimum is 2^n - 1 moves; the optimal sequence is fixed by the starting and ending pegs. With n=0, return an empty array.

Be ready to write both recursive and iterative Tower of Hanoi code; the editorial shows both.

Function

hanoiMoves(n: int) → String[]

Examples

Example 1

n = 2return = ["A->B","A->C","B->C"]

Move the small disk to B, move the large disk to C, then place the small disk on it. Three moves meet the lower bound 2^2 - 1.

Example 2

n = 3return = ["A->C","A->B","C->B","A->C","B->A","B->C","A->C"]

Move the top two disks from A to B in three moves, move the largest from A to C, then move the two smaller disks from B to C. The total is seven moves.

Constraints

  • For this exercise, assume 0 <= n <= 10.
  • All n disks start on peg A in size order. Pegs B and C start empty.
  • Return exactly 2^n - 1 moves as strings with one of the three uppercase peg labels on either side of ->.

More JP Morgan Chase Onsite Interview questions

  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
  • Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
  • K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
  • Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
  • Two SumONSITE INTERVIEW · Seen Nov 2025
  • Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
  • Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
  • Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024

Similar questions at other companies

  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Decode StringBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Basic CalculatorAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Flatten a Nested ArrayFreshworks · ONSITE INTERVIEWFreshworks interview questions
  • Verify Postorder Sequence of a BSTMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Evaluate an Arithmetic Expression with DivisionMotive · ONSITE INTERVIEWMotive interview questions
  • Reverse a Stack RecursivelyPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
  • Expand a Repeat-Encoded StringExpedia · ONSITE INTERVIEWExpedia interview questions

Interview reports that mention this question

  • JPMorgan Chase Intern Campus Rounds: 4 Experiences (Part 2 of 5)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public String[] hanoiMoves(int n) {
        // Write your code here.
    }
}
Checking account…