Code Challenge #22: September 6, 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 Twitter:
You have a bag containing tiles with numbers
[1, 2, 3, …, 100]written on them. Each number appears exactly once, so there are 100 tiles and 100 numbers. Now, without looking, one number tile is randomly picked out of the bag and discarded. Write a function,
missingNo, that will find the missing number.
Input: an array,
[1, 2, …, 100]with one number between 1 and 100 missing.
- Output: an integer, the “missing” number in the array
missingNo([1, 3, …, 100]) => 2if the array was missing the number
2. Please include in your submission a test array with number
- The array may not be sorted.
- The removal of a number from the bag is totally random, there is no way to tell what number it was by the process of removing it (e.g. you cannot feel what number is written on the tile)
- You may look in the bag of number tiles and interact with the contents once the random number is removed. So, you can lay all of the tiles out,
- Always remember to explain your code and the thought processes behind it!
- What if your interviewer had follow-up questions or extensions to this challenge? Don’t anticipate what exactly those follow-ups or changes may be, but try to write your code so that it is easily read, easily maintained, and can be adapted to potential modifications in the interviewer’s questioning.
Find out more about basic challenges.
You replace the missing number in the bag, so you again have 100 tiles numbered from 1 to 100. This time, you remove two random number tiles. Write a function,
missingNOs, that will find the missing numbers.
Input: an array,
[1, 2, …, 100]with two numbers between 1 and 100 missing.
- Output: an array of two integers, showing both “missing” numbers in the array in any order
missingNOs([1,4, …, 100) => [2, 3]
- Do you have to write a second script (i.e. write another version of
missingNOto solve this challenge, or could you simply generalize what you’ve made in the basic challenge?
- You must explain your submission to be able to win!
Find out more about intermediate challenges.
Write a function,
missingNoKetsuban, that will efficiently solve for a bag of size
n(array elements in range from
knumbers missing from the bag.
- You could think of this as a generalization of what you’d learn by solving the basic and intermediate level challenges.
- 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