Code Challenge #15: July 19, 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 Google:
Basic Difficulty
Write a function,
productOfTheOthers
that, when given an array ofn
integers, replaces each number in the array with the product of all the numbers in the array except the number itself.
- for example, when given the array
[1,2,3,4]
,productOfTheOthers
would return[24, 12, 8, 6]
- Function Name:
productOfTheOthers
- Input: an array – for your submission use the test array
[3, 9, 7, -2]
- Output: an array – when given the test array, your submission should return
[-126, -42, -54, 189]
- Example:
productOfTheOthers([3, 9, 7, -2]) => [-126, -42, -54, 189]
- Your array may have
n
integers, presume thatn > 1
. - Would your function work if the input array contained zeroes?
Find out more about basic challenges.
Intermediate difficulty
Solve the basic challenge, but this time do so without using division. Call this new function
advProductOfTheOthers
.
- Function Name:
advProductOfTheOthers
- Input: an array – for your submission use the test array
[0, 9, 7, 8, -2]
- Output: an array
- Example:
advProductOfTheOthers([1,2,3,4]) => [24, 12, 8, 6]
Find out more about intermediate challenges.
Hard Difficulty
Write
advProductOfTheOthers
as efficiently as possible.
Find out more about hard challenges and Big O
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.
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