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 are logical skills? 
4.2.
What are analytical skills?   
4.3.
What are puzzles?  
5.
Conclusion
Last Updated: Mar 27, 2024

Measure Liquid by Cans

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

This article will discuss a famous interview puzzle in which we have to draw every quantities of Liquid up to 40 liters using cans of different sizes from a drum full of Liquid. We can have a maximum of 4 cans; our task is to determine the capacity of cans so that every quantity between 1-and 40 can be drawn out using these four cans.  

Problem Statement

The problem states that we have a huge drum of Liquid, and we need to draw some liquid from it. To be precise, we need to measure every quantities of Liquid, between 1 - 40 liters using 4 cans. We need to determine the dimensions of these cans so that every quantity between 1 and 40 can be measured. Note: We can use every can only once. 

Suppose I need to measure 38 liters. I can have cans of sizes 1,3,9,27. The right way to measure 38 liters using these cans are 27 + 9 + 3 - 1 = 38. 

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

1 = 1
3 - 1 = 2
3 = 3

3 + 1 = 4
9 - 3 - 1 = 5
9 - 3 = 6
9 - 3 + 1 = 7
9 - 1 = 8
9 = 9

9 + 1 = 10
9 + 3 - 1 = 11
9 + 3 = 12
9 + 3 + 1 = 13
27 - 9 - 3 - 1 = 14
27 - 9 - 3 = 15
27 - 9 - 3 + 1 = 16
27 - 9 - 1 = 17
27 - 9 = 18
27 - 9 + 1 = 19
27 - 9 + 3 - 1 = 20
27 - 9 + 3 = 21
27 - 9 + 3 + 1 = 22
27 - 3 - 1 = 23
27 - 3 = 24
27 - 3 + 1 = 25
27 - 1 = 26
27 = 27

27 + 1 = 28
27 + 3 - 1 = 29
27 + 3 = 30
27 + 3 + 1 = 31
27 + 9 - 3 - 1 = 32
27 + 9 - 3 = 33
27 + 9 - 3 + 1 = 34
27 + 9 - 1 = 35
27 + 9 = 36
27 + 9 + 1 = 37
27 + 9 + 3 - 1 = 38
27 + 9 + 3 = 39
27 + 9 + 3 + 1 = 40

 

The right approach to think of a solution for this problem is this. If you have X cans that let you measure the quantities between 1 to N, now you will include 2*N + 1 as your next can because it has some very good advantages. 

  1. It will let you measure all quantities between N+1 to 2*N by subtracting N to 1, respectively(we already can measure this quantity using previous X cans) from a new can(2*N+1). 
  2. It will also let you measure quantities between 2*N+2 to 3*N+1 by adding 1 to N(we already can measure these quantities using previous X cans) from a new can(2*N+1). 

 

We need to start with 1, as it is mandatory to measure quantity 1, so now, according to the above logic, the next can should be 3(you can measure up to 4), the next one should be 9(you can measure up to 13), and the fourth one should be 27. 

As shown above, these cans can measure all quantities between 1 and 40. 

Frequently Asked Questions

What are logical skills? 

Analyzing a situation and coming up with a reasonable solution is also known as logical thinking. It's a lot like critical thinking. Logical thinking employs reasoning abilities to objectively examine any issue, allowing for a sensible decision on how to continue. For example, if you're dealing with a problem at work, you'll utilize the information at hand and your logical thinking abilities to solve it.

What are analytical skills?   

Analytical abilities allow you to recognize the complexity of a problem, analyze and organize it, solve it, make projections, and generate new ideas. Making accurate assessments of events and offering insight into how various components interact is what analytical thinking entails. As a result, analytical skills are highly directed toward understanding cause and effect and forecasting the implications of possible solutions.

What are puzzles?  

A puzzle is a game, problem, or toy that challenges one's imagination or understanding. The solver is meant to arrange pieces logically to arrive at the correct or pleasurable answer to a puzzle.

Conclusion

This article briefly discussed the puzzle to find the dimensions of cans to measure each quantity between 1 and 40. 

We hope you understand the problem and solution properly. Now you can do more similar questions.

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 Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, 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!

Previous article
Train Station
Next article
Cross Bridge II
Live masterclass