Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Tower of Hanoi Implementation
00:00
5 left

Tower of Hanoi Implementation

EasyPython

Problem

Coupa workflow configuration may need to explain a sequence of dependent operations. Implement the Tower of Hanoi algorithm to generate the optimal sequence of disk moves for such a dependency chain.

You are given n disks arranged from largest at the bottom to smallest at the top on a source peg. Move all disks to a target peg using an auxiliary peg.

A move transfers exactly one top disk from one peg to another. A larger disk may never be placed on a smaller disk. Return the complete sequence of moves in execution order.

Formal Specification

Implement solve_hanoi(n, source, auxiliary, target). n is a non-negative integer, and each peg name is a string. Return a list of tuples, where each tuple has the form (from_peg, to_peg). The sequence must use the minimum possible number of moves, 2^n - 1 for n > 0.

The function must not mutate any caller-provided object and must handle n = 0 by returning an empty list.

Constraints

  • 0 <= n <= 15
  • Peg names are non-empty strings
  • The three peg names are distinct
  • Return exactly 2^n - 1 moves when n > 0
  • Each move is a tuple of two peg names

Function Signature

def solve_hanoi(n, source, auxiliary, target):
Interviewer

Your question is Tower of Hanoi Implementation. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.