Color of the last ball — Puzzle for Interview rounds

Question :

You have 20 blue balls and 13 red balls in a bag.

You put your hand in and remove 2 at a time.

1. If they’re of the same color, you add a blue ball to the bag.

2. If they’re of different colors, you add a red ball to the bag.

Assume you have a big supply of blue & red balls for this purpose.

Note: When you take the two balls out, you don’t put them back in, so the number of balls in the bag keeps decreasing.

What will be the color of the last ball left in the bag?

Solution :

There are 3 possible cases of removing the two balls…
a) If we take off 1 RED and 1 BLUE, in fact we will take off 1 BLUE
b)If we take off 2 RED, in fact we will take off 2 RED (and add 1 BLUE)
c) If we take off 2 BLUE, in fact we will take off 1 BLUE
So In case of A) or C), we are only removing one blue ball, but we always take off red balls two by two.

Now as the no. of red balls is odd, there will be one single red ball in the bag with other blue balls, and whenever we remove 1 red and 1 blue ball, we end up taking off only the blue ball. So the red ball will be the last ball in the bag.

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

Terraform Module for Kubernetes Cluster with Google Anthos

To get the reproduction number down to 1 as the new strain becomes the dominant one

Programming 101: Declarative vs Imperative Paradigm

A simple Tableau Server user and group management framework that scales

5 Fancy Shells You Should Give a Try

Tools

Creating Stages in API Gateway& Adding Alias in Lambda.

Technical Updates

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

Two Pills Each Day

Knight Probability in Chessboard Solution | LeetCode-688: Medium | JavaScript Implementation

Understanding Time and Space Complexity

Sliding Window Technique