Introduction
Solving puzzles for coding interviews demands a strategic approach that combines problem-solving skills, coding proficiency, and structured thinking. These puzzles often assess a candidate’s ability to tackle complex problems and design efficient algorithms under time constraints.
To effectively solve puzzles for coding interviews, aspiring candidates must employ a systematic methodology. This involves understanding the problem thoroughly by analyzing constraints, requirements, and potential edge cases. Next, devising a clear strategy or algorithmic approach significantly aids in solving the puzzle efficiently. This could involve breaking down the problem into smaller subproblems or applying known algorithms or data structures.
Moreover, practicing regularly on platforms that offer coding challenges and puzzles helps sharpen problem-solving skills, enhances familiarity with various problem types, and builds confidence in handling unexpected challenges during interviews. By honing problem-solving techniques and consistently practicing, candidates can better prepare themselves to ace coding interviews featuring puzzles and algorithmic challenges.
Prerequisites
Let us learn how to solve puzzles for coding interviews with 8 eggs example
Puzzles are problems that on the surface seem easy but are not. Let us understand this with an example.
Question:- There are 8 eggs, all weighing the same except one(heavier) and you have a balance. In how many rounds of weighing we can find the heavier egg?
Whenever we are faced with such problems we tend to apply the divide and conquer approach and think this will give the best answer.
Let us see the problem with this approach. For that, we will apply this approach and see the results.
Solution: 1st approach (Using Divide and Conquer)
1st weighing, divide the eggs in 4-4 groups each and weigh, keeping the heavier 4-egg group.
2nd weighing, divide the eggs(4 heavier eggs from the previous weighing) in 2-2 groups each and weigh, keeping the heavier 2-egg group.
3rd weighing, divide the eggs(2 heavier eggs from previous weighing) in 1-1 group each and weigh, result = heavier egg.
In this approach, the result will be 3 and it also seems to be the best optimal and we can’t think of any other approach (this makes the puzzle difficult).
2nd Approach
Let us divide the eggs in 3 groups 3 eggs-3 eggs-2 eggs
1st weighing, weight 3egg-3egg.
a) if no one is heavier then it means the heavier egg is in the last 2-eggs group and we will divide this last 2-egg group in 1egg-1egg and weigh them against each other and we will get the heavier egg in the 2nd round only.
b) if one of them is heavier then we will divide this egg in 3 groups of 1egg-1egg-1egg each. Now we will pick any two groups from this and weigh them against each other. If one of them is heavier we got this ( in 2nd round only). If they are the same then it means the rest 1egg that we kept aside is the heavier (in 2nd round only, no extra weighing required.
Overall 2nd approach solves this in 2 rounds only.
The second solution puts this problem in the category of puzzles because we can’t come up with such a solution in the first place but why??
the difference between the above two approaches is in the initial division of eggs (4-4 and 3-3-2) and this is the key to the optimal solution.
Can we use some information from the problem statement to our advantage? and the answer is yes.
The problem states that only 1 egg is heavier =>(implies) the other 7 eggs have the same weight.
but we can’t compare these 7 eggs, not an issue, we have one more logical relation.
1 egg is heavier =>(implies) other 7 eggs have same weight. => (implies) 6 eggs have the same weight.
We can compare these 6 eggs on a balance having 3 eggs on each side and from here we can apply the second approach.
Such problems are keys to understanding a person’s rational thinking and their strength to apply knowledge to new problems. We should not fear such problem but embrace them. Here also, intelligent practice can help you build your puzzle-solving skills.
Conclusion
Puzzles are puzzles only on the surface. Once we learn how to break apart different pieces of puzzles, solve them in isolation and combine them all to solve it, then it just becomes fun to solve puzzles for coding interviews.
Please enjoy the post.
1 thought on “Master How to Solve Puzzles for Coding Interviews: A Simple approach with 8 eggs puzzle example”