Back to JavaScript
2026-04-096 min read

Puzzles

Learn Puzzles step by step with clear examples and exercises.

Why This Matters

Puzzles are a fun and exciting way to challenge your brain and improve problem-solving skills, often used in exams and job interviews to test logical and analytical abilities. Whether you're a student preparing for tests or just looking for a mental workout, solving puzzles can be both enjoyable and beneficial.

In this lesson, we will focus on JavaScript puzzles that are commonly asked during interviews, helping you build a strong foundation for your problem-solving skills.

Prerequisites

To follow along with this lesson, you should have a basic understanding of the following:

  • JavaScript syntax and variables
  • Loops (for loops and while loops)
  • Functions
  • Arrays
  • Objects

Core Concept

In this section, we will cover various types of JavaScript puzzles that are commonly asked during interviews. We'll provide solutions and explain the logic behind each puzzle.

Monty Hall Problem

The Monty Hall problem is a probability puzzle involving three doors. Initially, you choose one door, but before it is opened, the host opens another door with a goat behind it and asks if you want to switch your choice to the remaining unopened door. The question is whether switching doors increases your chances of winning the car (which is behind one of the three doors).

Here's a JavaScript implementation of the Monty Hall problem:

function montyHall(numDoors, myChoice, hostChoice) {
const cars = [myChoice];
let unopenedDoors = numDoors - 1;

while (unopenedDoors > 0) {
const openDoor = Math.floor(Math.random() * unopenedDoors);

if (openDoor !== hostChoice && openDoor !== myChoice) {
cars.push(openDoor);
unopenedDoors--;
}
}

const carIndex = Math.floor(Math.random() * cars.length);
return cars[carIndex] === myChoice;
}

In this function, we create an array cars that initially contains only the door you chose (myChoice). We then loop through the remaining unopened doors and add them to the cars array if they are not the host's choice or your initial choice. After all unopened doors have been added, we randomly select the car's location from the cars array and return whether you won (i.e., whether the car is behind the door you initially chose).

986 Game

The 986 game is a simple number puzzle where you are given two numbers, A and B, such that A + B = 986, and you need to find two integers X and Y that satisfy the equation: (A - X) * (B - Y) = 120.

Here's a JavaScript implementation of the 986 game:

function ninetyEightSix(a, b) {
for (let x = 1; x <= a; x++) {
for (let y = 1; y <= b; y++) {
if ((a - x) * (b - y) === 120) {
return [x, y];
}
}
}
}

In this function, we loop through all possible values of X from 1 to A and all possible values of Y from 1 to B. For each pair (X, Y), we check whether the product (a - x) * (b - y) equals 120. If it does, we return the found solution as an array containing X and Y.

Hanoi Towers

The Tower of Hanoi is a classic problem that involves moving disks from one peg to another according to specific rules. The goal is to move all disks from the initial peg (peg A) to the destination peg (peg C), using the spare peg (peg B) as an intermediate step, while obeying these rules:

  • Only one disk can be moved at a time.
  • Each move consists of moving one disk.
  • A larger disk cannot be placed on top of a smaller one.

Here's a JavaScript implementation of the Hanoi Towers problem:

function hanoiTowers(n, source, target, auxiliary) {
if (n === 1) {
console.log(`Move disk 1 from ${source} to ${target}`);
return;
}

// Move n - 1 disks from the source peg to the auxiliary peg
hanoiTowers(n - 1, source, auxiliary, target);

// Move the largest disk (disk n) from the source peg to the destination peg
console.log(`Move disk ${n} from ${source} to ${target}`);

// Move n - 1 disks from the auxiliary peg to the destination peg
hanoiTowers(n - 1, auxiliary, target, source);
}

In this function, we recursively solve the Tower of Hanoi problem for a given number of disks (n). If there is only one disk, we print the move and return. Otherwise, we first move n - 1 smaller disks from the source peg to the auxiliary peg using a recursive call. After that, we move the largest disk (disk n) from the source peg to the destination peg. Finally, we move n - 1 smaller disks from the auxiliary peg to the destination peg using another recursive call.

Worked Example

Let's solve a Monty Hall problem with 3 doors:

montyHall(3, 1, Math.floor(Math.random() * 3));

This code creates a function that simulates the Monty Hall problem with 3 doors and runs it with door 1 as your initial choice and a randomly chosen host's choice. The output will be either true (you won) or false (you lost).

Common Mistakes

When solving puzzles, common mistakes include:

  • Misunderstanding the problem statement or making incorrect assumptions
  • Failing to consider edge cases
  • Overcomplicating the solution by using unnecessary variables or loops
  • Making careless errors in the code, such as typos or logical errors

Monty Hall Problem

  1. Assuming that switching doors always increases your chances of winning
  2. Not considering the case where the host initially chooses the car (in which case switching does not matter)
  3. Miscalculating probabilities due to incorrect understanding of conditional probability

986 Game

  1. Failing to consider all possible values for X and Y within the given range
  2. Making a mistake in the multiplication calculation
  3. Overlooking edge cases where A or B is equal to 120 (in which case no solution exists)

Hanoi Towers

  1. Forgetting one of the rules, such as moving more than one disk at a time or placing a larger disk on top of a smaller one
  2. Failing to handle edge cases, such as moving 0 disks or moving all disks in a single recursive call
  3. Writing an inefficient solution that does not take advantage of the problem's structure

Practice Questions

  1. Solve the Monty Hall problem with 5 doors and your initial choice as door 2, given that the host initially chooses door 4.
  2. Solve the 986 game for A = 30 and B = 70.
  3. Solve the Tower of Hanoi problem for 5 disks, starting with all disks on peg A, and moving them to peg C using peg B as an intermediate step.

FAQ

The Monty Hall problem is a probability puzzle involving three doors. Initially, you choose one door, but before it is opened, the host opens another door with a goat behind it and asks if you want to switch your choice to the remaining unopened door. Switching doors increases your chances of winning because there was a 1/3 chance that the car was behind the door you initially chose, and a 2/3 chance that it was behind one of the other two doors. By switching, you effectively double your chances of choosing the correct door from 1/3 to 2/3.

How can I improve my problem-solving skills for puzzles like these?

Practicing regularly and working on a variety of puzzles can help improve your problem-solving skills. Additionally, learning and understanding basic concepts in mathematics, logic, and computer science can provide a strong foundation for solving complex puzzles.

Are there any books or resources that can help me prepare for puzzle-based interviews?

Yes, there are several books and resources available to help you prepare for puzzle-based interviews:

  • "Cracking the Coding Interview" by Gayle Laakmann McDowell
  • "Programming Pearls" by Jon Bentley
  • "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
  • Websites like Project Euler (https://projecteuler.net/) and LeetCode (https://leetcode.com/) offer a wide range of programming problems, including puzzles, that can help you practice and improve your problem-solving skills.
Puzzles | JavaScript | XQA Learn