Code Challenge #21: August 30, 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 Facebook and has also been asked right here at Codecademy!
- Input: a 2D array
- Output: a 1D array
flattenArray([1,2,3, [4,5], 6, [7,8], 9]) => [1,2,3,4,5,6,7,8,9]
- Always remember to explain your code and the thought processes behind it!
- You can think of a 2D array as a spreadsheet or a chessboard, whereas a 1D array is more like a list or one long chain of data.
- 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.
Improve on the
flattenArrayfunction by writing
flattenArrayN, a function that can flatten arrays that are nested
n-levels deep, returning a flattened 1D array.
Input: any array with
nlevels of depth, where
nis an integer
- Output: a 1D array
flattenArrayN([1, 2, [3, [4, 5]], 6]) => [1, 2, 3, 4, 5, 6]
- For our intermediate challenge, the array can have multiple types:
null, and integers
(1,2,3,…)are all valid types inside the array.
- You must explain your submission to be able to win!
Find out more about intermediate challenges.
flattenArrayNas efficiently as possible.
- 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