I have a number of questions in my quiver when I'm giving technical interviews to candidates. They are all of a similar flavor. I don't ask puzzle questions, I find them low value. Instead, I ask questions that are straightforward but have a few angles with which to explore deeper topics.
Enter the humble median.
Write a function that takes an array of numbers and returns the median.
Here's a Python implementation with discussion comments.
def median(numbers: list[float]) -> float: # What should we do if the list is empty? # Raise an exception? Return a sentinel value? if not numbers: raise ValueError("median called with empty list") # Python is pass-by-reference, what are the # implications of sorted() vs numbers.sort()? numbers = sorted(numbers) length = len(numbers) mid = length // 2 # High-quality candidates will bravely import 1,000 # dependencies to get an is_even library func. if length % 2 == 0: return (numbers[mid - 1] + numbers[mid]) / 2.0 else: return numbers[mid]