#Code Challenge #7: May 24, 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 reportedly asked in interviews at Facebook:
Create a program
makeNumberBasic(z)which, when given an input of a number (
z), returns the number of all possible permutations of digits (
9inclusive) that when added will equal
- For example, if
3, your program will find that four permutations of digits add up to that value (
1+2), and thus return
- You may limit
makeNumberBasic(z)to the use of five digits
- Repeat use of a digit is acceptable: e.g.
1+1+1would be a valid addition of digits equalling
- Use of a single digit is acceptable as a permutation: e.g.
3is itself a valid permutation of digits that add up to
makeNumberBasic(z)is looking for permutations, not combinations:
5+1would count as two unique possible ways to add to
6, not one. Read more about permutations vs. combinations here.
- If no permutations of the digits
9add up to the number
z, your function should return
#####Find out more about basic challenges.
makeNumber(z)so that it only returns the number of combinations of unique digits from
- For example, if
3, your program will find the unique combinations
1+2, and thus return
- To clarify unique digits: repeat use of a digit is no longer acceptable.
makeNumber(z)would not consider
3+3a valid combination as it uses the digit
3more than once.
makeNumber(z)is now looking for combinations not permutations:
5+1would now only be accepted as one possible way to add digits
6, not two.
#####Find out more about intermediate challenges.
Write versions of
makeNumber(z)(the normal and intermediate difficulty challenges) as efficiently as possible.
@lmreia had a great use of recursion and needed the fewest “runs” through the main problem of the body to solve, making it faster than other recursion solutions. See their solution here and our comments here.
####Scroll down and
reply to this thread with your code to participate! Don’t just submit your code, remember to
explain your solution, too! it is now too late to be considered under our “best code” assessment, but please include a link to your code running on repl.it and abide by our other simple rules.
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