Code Challenge #14: July 12, 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 Amazon:
Basic Difficulty
Write a function,
getMedian
, that will return the median value of a list of numbers.
- Function Name:
getMedian
- Input: An unsorted
List
ofintegers
- Output: The
integer
which is the median value of the input list. - Examples:
getMedian([6,10,2,5,9,3,10,12,18,-3]) => 7.5
(average of the middle two values)
getMedian([5,10,-3,7,9]) => 7
Find out more about basic challenges.
Intermediate difficulty
Write a function,
getX
, that given an integerx
and a list returns theXth
number if the list was in sorted order.
- Function Name:
getX
- Input: An
integer
, x, and an unsortedlist of integers
- Output: The
integer
corresponding to theXth
number in the sorted list - Example:
getX(2, [5,10,-3,7,9]) => 5
getX(4, [5,10,-3,7,9]) => 9
- Note that this assumes the first number is position
1
not0
.
Find out more about intermediate challenges.
Hard Difficulty
Write
getX
so that the list is not simply sorted and then theXth
element picked out. It is possible to solve this problem in linear time,O(n)
.
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