Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem
3.
Solution
4.
Frequently Asked Questions
4.1.
Why are puzzles important?
4.2.
Are puzzles asked in the interview of IT companies?
4.3.
Do I need to memorize all the puzzles?
4.4.
What if there were 20 Blue balls and 13 Red balls?
4.5.
How is the number of balls reduced in the bag if we are always adding a ball?
5.
Conclusion
Last Updated: Mar 27, 2024

Color of the Last Ball

Interview Puzzles

Introduction

A puzzle is a game or problem that trains our brain. Puzzles train our brains and help us develop problem-solving skills. We have discussed the Color of the last ball puzzle in this article. After reading the problem statement, try solving the puzzle, then only move to the solution part. This way, you will understand the concept behind this puzzle. So let’s begin.

Problem

We have 20 blue balls and 14 red balls in a bag.

We put our hands in and removed two balls at a time.

Two conditions arise while removing the balls.

  • Both balls are of the same Color. In this case, we add a blue ball to the bag.
  • Both balls have different colors. In this case, we add a red ball to the bag.

We can assume that we have an unlimited supply of balls of both colors.

Note: We don’t put it back when we remove any ball. So the overall number of balls reduces.

We have to find out the Color of the last ball left in the bag.

illustration image

Solution

The first step is to think of different cases while removing the balls. There will be three separate cases to remove the balls from the bag.

Case-1: Both balls are Red. It is mentioned in the question that if balls are of the same Color, then we add a blue ball to the bag. So, we are removing 2 Red balls and adding 1 Blue ball.

-2R +1B

‘‘R’’ represents Red color ball, and ‘‘B’’ represents a blue ball.

Case-2: Both balls are Blue. Since the balls are of the same Color, we add a blue ball back to the bag. So, we are removing two blue balls and adding one blue ball in this case. Overall we can say that we are subtracting one blue ball.

-2B +1B = -1B

Case-3: One ball is red, and another ball is blue. If colors are different, then we add a red color ball to the bag. So, overall, we are removing one blue ball here.

-1R -1B +1R = -1B

Now, two scenarios can arise while removing the balls.

  • Scenario-1: All Red balls are removed, and some blue balls are left.
    If there are only blue balls left, then we get two blue balls every time we remove the balls. This falls in case-2.
    -2B +1B = -1B
    So, what is happening here is that the number of blue balls is reduced one by one. Only one blue ball will undoubtedly be left in the bag.
  • Scenario-2: All blue balls are removed, and some red balls are left.
    If there are only red balls, then we will get two red balls every time we remove the balls. This falls in case-1
    -2R +1B
    Overall, we have added a blue ball and removed two red balls. Now, again scenario-1 and scenario-2 can arise. Again this process will repeat. At the end of scenario-2, there will be two red balls left, and when we remove the two red balls, we add a blue ball. So finally, the blue ball is left at the end.

We can see that we are always left with a blue ball, and indeed, one of the two scenarios will always arise.

Let’s suppose you think that there can be a case where only one red ball and one blue ball are left in the bag. Then it falls under case-3, and after removing these two balls, we will have to add a red ball. So here, a red ball is left in the end. This is the only possibility where a Red ball is left out in the end.

-1R -1B +1R = -1B

So 1R is left in the bag.

But, this case will never arise. The number of red balls is reduced only when case-1 occurs. And in case-1, the number of red balls is reduced in pairs, i.e., even number of red balls is always reduced. Now, the given number of red balls is even, and we know that subtracting even from even always results in even. So we can conclude that the number of red balls in the bag will be even at any point in time. So, no such case will arise where only one red ball is left in the bag.

So the answer is Blue.

What if there were 20 Blue balls and 13 Red balls?

Try to solve this on your own. You can check out the Color of the Last Ball II article for its solution.

Check out this problem - 8 Queens Problem

Frequently Asked Questions

Why are puzzles important?

Puzzles enhance your ability to think. It also improves your problem-solving skills. You will have to apply your mind to solve a puzzle, so it’s also a good exercise for the mind. You will improve your logical thinking and reasoning ability by solving puzzles.

Are puzzles asked in the interview of IT companies?

Many tech giants like Google, Microsoft, etc., ask the puzzles in the interview process as they want to check your logical thinking.

Do I need to memorize all the puzzles?

No, when you try solving a puzzle yourself, it gets stuck in your memory for a long time as you have struggled to solve it. Even if you couldn’t solve it yourself, you won’t forget it for a long time as you realize the mistakes you made while solving it.

What if there were 20 Blue balls and 13 Red balls?

In this case, a single red ball will be left out.

How is the number of balls reduced in the bag if we are always adding a ball?

The number of balls is reducing since we are removing two balls and adding a single ball. So after every operation, the total number of balls is reduced by one.

Conclusion

We have extensively discussed the Arrange Cubes puzzle.

We hope that this blog has helped you understand this puzzle, and if you would like to solve more puzzles, check out these articles. 


Check out some of the amazing Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Basics of C, Basics of Java, Computer Networks, etc. along with some Contests and Interview Experiences only on Coding Ninjas Studio

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow.

Happy Learning!!!

Live masterclass