Over a million developers have joined DZone.

Thursday Code Puzzler: Solve the Towers of Hanoi

DZone's Guide to

Thursday Code Puzzler: Solve the Towers of Hanoi

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Another Thursday, another puzzle. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you think is suitable. Last week we got some great answers with Fabie, Christian and Francesco giving pretty detailed answers - thanks for participating guys. 

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It's all about the participation!

Towers of Hanoi 

Let's go for another classic computer science coding problem, the Towers of Hanoi.  The objective is to move the entire stack to any other rod, obeying these rules: 

  • Only one disk can be moved at a time
  • Each move consists of taking the upper disk from one rod, adding it onto another rod, on top of any disks that you may have already placed on that rod.
  • But no disk can be placed on top of a smaller disk

There's a few different ways to achieve this. If you wish, you can express the efficiency of the algorithm you come up with using Big O notation.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}