Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
4.
Frequently Asked Questions
4.1.
What is the definition of a puzzle interview?
4.2.
What are the advantages of solving puzzles?
4.3.
How to solve a number sequence puzzle?
5.
Conclusion
Last Updated: Mar 27, 2024

Color of the Last Ball II

Author Divyansh Jain
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

Color of the Last Ball is a very famous problem. The problem along with the solution is given below. Read the question and try to attempt it on your own. Refer to the solution only when you are not able to do the question. The solution will explain to you the concept behind the question. Interesting!, isn’t it? So let's get started.

Problem Statement

In a bag, you have 10 blue balls and 17 red balls. You reach in and pull out two at a time.

  • You put a blue ball in the bag if they're the same color.
     
  • If the balls are of different colors, a red ball is added to the bag.

Assume you have a large quantity of blue and red balls. 

Note: Since you don't put the two balls back in the bag as you take them out, the number of balls in the bag continues to decrease.

illustration image

What color will be the final ball in the bag?

  1. Red
  2. Blue
  3. Can not be determined

Now, give a thought to the question and it may look easy to you or maybe not. If not, no need to worry, you can check out the solution when attempted properly. So, shall we begin?

illustration image
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Solution

Take all the three cases {(blue, blue),(red, red),(blue, red)} and state the change that happened in the bag after the replacement. 

S.No Balls Pulled Out Balls Submitted back  Net Change in the bag
1. 2 blue 1 blue ball  -1 blue ball
2. 2 red  1 blue ball -2 red ball +1 blue ball
3. 1 blue, 1 red 1 red ball -1 blue ball

 

Now on observing the net change that takes place in each case we can see that for the red balls in case no 2. Two balls can be removed and there is no other case where a change in the number of red balls takes place. 

Red balls can be removed in the multiples of two hence there is no possible combination in which an odd number of red balls can be taken out. In the question, we have an odd number of red balls(17) so at most 16 red balls can be removed and there is no way to remove the last remaining red ball. 

Hence 1 red ball will be left in the bag.

Check out this problem - Shortest Common Supersequence.

Frequently Asked Questions

What is the definition of a puzzle interview?

The so-called "puzzle interview," popularized by Microsoft in the 1990s, is the most renowned example of this trend. Puzzle interviews require the applicant to answer riddles (for example, "Why are manhole covers round?") or address odd situations (for example, "How would you weigh an airplane without a scale?").

What are the advantages of solving puzzles?

Solving puzzles enhances the problem-solving skills of a person. It also improves the logic building of a person.

How to solve a number sequence puzzle?

To solve a number sequence puzzle, first, find the pattern between numbers of the sequence. Once you know the pattern then you can find the next number of the sequence by following the same pattern.

Conclusion

To summarize the article, we have discussed the famous Color of the balls problem and the solution regarding the same. Also, we have discussed some frequent doubts regarding the subject.

Logical Puzzles:


Pattern Based Puzzles:


Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, 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.

Happy Learning Ninja :) 

Previous article
Dinner Hand Shakes
Next article
Measure Time by Hourglass
Live masterclass