Code Challenge #18: August 9, 2017
Every week, we feature the type of brain-teasing question that might be asked in a full-stack developer’s job interview at places such as Google and Facebook.
This week’s challenge was reported to have been asked in interviews at Microsoft:
Suppose you were given eight soccer balls (aka footballs ), all of them seemingly identical. You are given a balance scale and told that one of the eight balls is slightly heavier than the others (
outlierBall). What’s the fewest number of times you have to use the scale to find
outlierBall? Write a function,
scaleOfTruth, that will determine the minimum number of weighs that you’ll need to find
- Input: None
Output: an integer representing the minimum number of weighs needed to find
- Seven of the footballs have an exactly even weight, only one of the footballs (
outlierBall) has a greater weight.
- The balance scale is the only way that you can discern any physical difference between the eight footballs.
- The balance scale did not come with any reference weights – the only weights you have to use on the balance scale are the footballs themselves.
- The balance scale is very large – you can fit all eight balls on one side of the scale! Thus, you can put multiple footballs on each side when you weigh, if you so choose.
- What if you were given a ninth football with an identical weight to the eight equally heavy balls you have already? What if
outlierBallwas lighter than all the others rather than heavier? Try to write your code so that it is easily maintainable and can be adapted to modifications in the interviewer’s questioning.
- Always remember to explain your code and the thought processes behind it!
Find out more about basic challenges.
Write a function,
scaleOfTruthNthat will solve this challenge with any given number of balls,
n, and make sure that your function is as efficient as possible.
- This function should print out the minimum number of weighs needed to find the
outlierBallin a set of
nfootballs, where all balls have identical weights apart from one
outlierBall, which is heavier.
- Input: n - an integer representing the number of footballs
- Output: An integer representing the minium number of weighs to find the outlier ball.
- Don’t forget to explain your submission just as you would do in a job interview setting!
Reply to this thread with a link to your code on repl.it and paste your properly formatted code to participate! Don’t just submit your code, remember to
explain your solution, too! If you want to be considered as the winner, your function
must have the
correct name and provide
output in the format specified above, you also need to abide by our other simple rules.
As always solutions using imports to do all the heavy lifting such as
itertools will not be considered for the winner.
When including your repl.it link, please just give us the link and not the code preview! Just put a space before your URL so that the preview box doesn’t show – too many preview boxes can make the thread hard to read.
The fine print:
Click the links to find out more about:
- the rules & how to participate in challenges
- how challenges are used as job interview questions
- why Codecademy runs challenges (and why they are formatted this way)
- more details about the challenges and why we think they are useful.
- find previous challenges (and see the past winners) in our Challenge Index