Monkey Door Puzzle-Asked in Amazon Interviews

Question:

There are 100 doors, all closed.
In a nearby cage are 100 monkeys.

The first monkey is let out, and runs along the doors opening every one.
The second monkey is then let out, and runs along the doors closing the 2nd, 4th, 6th,… - all the even-numbered doors.
The third monkey is let out. He attends only to the 3rd, 6th, 9th,… doors (every third door, in other words), closing any that is open and opening any that is closed, and so on.

Solution: 10

Consider door number 56, monkeys will visit it for every divisor it has. So 56 has 1 & 56, 2 & 28, 4 & 14, 7 & 8. So on pass 1 1st monkey will open the door, pass 2 2nd one will close it, pass 4 open, pass 7 close, pass 8 open, pass 14 close, pass 28 open, pass 56 close. For every pair of divisors the door will just end up back in its initial state. But there are cases in which the pair of divisor has same number for example door number 16. 16 has the divisors 1 & 16, 2 & 8, 4&4. But 4 is repeated because 16 is a perfect square, so you will only visit door number 16, on pass 1, 2, 4, 8 and 16… leaving it open at the end. So only perfect square doors will be open at the end. 

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

--

Upskilling students for tech placements!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Learning How to Learn — A guide to navigating a self-paced coding bootcamp

picture of lightbulb illuminating a book

How Kubernetes is helping Bose to solve its challenges

software-rfp.com

Understanding Basic OOP

[C++筆記]

The Big Game — but Bob and Alice just don’t trust each other or, in fact, anyone: The Mental Poker…

A simpler, more reliable approach to high availability in the cloud

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Placewit

Placewit

Upskilling students for tech placements!

More from Medium

King and Poison Puzzle

Goldman Sachs — SDE

InMobi Interview Experience for SDE-I (on-campus)

Oracle Interview Experience (On-Campus)