a software engineering interview question I like: computing the median

Lobsters Hottest News

Summary

The author describes their favorite software engineering interview question: computing the median of an array. They discuss the various angles it offers for evaluating candidates' programming skills, including sorting, edge cases, API design, and statistical understanding.

<p><a href="https://lobste.rs/s/8kxouk/software_engineering_interview">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 07/08/26, 04:24 AM

# Kris Shamloo Source: [https://krisshamloo.com/blog/007](https://krisshamloo.com/blog/007) ### [a software engineering interview question I like: computing the median](https://krisshamloo.com/blog/007) 2026\-05\-03 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\. - At a minimum: this will give you a "Fizz Buzz"\-like signal that the candidate can actually program\. Reducing an array of values is table stakes\. - Right out the gate: the numbers must be sorted\. Should the function sort them? Should the caller? If the array was passed in by reference is it ok to mutate? How does the API design influence the performance? - It has an off\-by\-one trap\. Mind you I don't care if anyone falls into an off\-by\-one trap, I fall into them all the time, but you'll often get a chance to watch someone debug a small problem\. - It has a branch: an even length array versus an odd one\. - It can lead to some discussion about statistics and why you might prefer a median to a mean in most cases\. - Gives the candidate a chance for some extra points by being so readily testable\. - Gives the candidate a chance to display some standard library knowledge via[statistics](https://docs.python.org/3/library/statistics.html#module-statistics)\. Here's a Python implementation with discussion comments\. ``` def median(numbers: list[float]) -> float: if not numbers: raise ValueError("median called with empty list") numbers = sorted(numbers) length = len(numbers) mid = length // 2 if length % 2 == 0: return (numbers[mid - 1] + numbers[mid]) / 2.0 else: return numbers[mid] ```

Similar Articles

jwasham/coding-interview-university

GitHub Trending (daily)

A comprehensive, multi-month study plan for software engineering interviews at major tech companies, originally created by John Washam and widely adopted by the developer community.

@vintcessun: Came across an interesting project this morning that changed my perspective on interview prep. I used to think that grinding LeetCode was enough for big tech interviews, but in essence, they test your complete understanding of computer science. This project strings together discrete knowledge points into a systematic study plan, covering everything from Big-O, data structures, algorithms to system design and interview tips, even including how to write a resume.

X AI KOLs Timeline

A popular GitHub project providing a comprehensive multi-month study plan for software engineering interviews, covering CS fundamentals, algorithms, system design, and resume tips.