logoalt Hacker News

A software engineering interview question I like: computing the median

61 pointsby speckxtoday at 12:38 AM76 commentsview on HN

Comments

lubujacksontoday at 3:53 AM

This question, and many "but make it a bit more challenging!" comments always strike me as CS101 navelgazing type questions. The best part of this question is that it is simple and can be used to swing into deeper concerns but it is still at odds with actual job responsibilities - even more so with LLMs in the mix.

Maybe this is because I have only worked at startups, but I am much more interested in if someone can read and understand code, where they feel logic is brittle, overly complex or badly designed. If they understand, even conceptually, how adding an optional field to an endpoint may be fine but removing one needs to be phased out or considered for active users. If they consider downstream risks, if they understand business goals and how to communicate limitations or opportunities.

Instead, every single tech interview seems to focus on how well you paid attention in your CS seminar which might be a reasonable screen for junior employees but is awfully irrelevant for anyone >3 years in the industry.

There are far too many corners of logic for everyone to know. Maybe someone has never dealt with data streams, or even forgets what a median is. You want to know if they are sharp with statistics? Great for some roles, wholly irrelevant for many others.

Engineers need to communicate, read and understand logic and how things connect. And the golden skill: willing and able to learn something new.

show 3 replies
apricottoday at 1:18 AM

In addition to the points listed, it gives the algorithm nerds the opportunity to show their overqualification by whipping out the O(n) median algorithm and proving that it works in linear time.

show 2 replies
cgearharttoday at 2:53 AM

We used to use this, but it was a broader conversation around tradeoffs to meet different constraints. If the expected array is small, then sort + index is probably fine. If it’s big (bigger than main memory?) and latency is the most important then maybe you want median-of-medians. If it’s a stream and you want to keep memory fixed then you might want a sketching algorithm. If I suggest that we can bound the error of the median estimate with constant additional space and the same complexity, would you believe me? (Just track the mean and standard deviation.)

Honestly, when I ran this interview I didn’t care much about the specifics of what you memorized beforehand. I care if you can read and write code a bit. I care more whether we can have a productive conversation. If you learn something new from me or the problem, how does that look and feel? If I make a mistake, how do you react? Are we able to communicate technical ideas to each other? Are we able to productively work through conflict?

We’re not computing many medians day-to-day, but we’re doing all those other things constantly.

nitwit005today at 2:30 AM

> Right out the gate: the numbers must be sorted.

I was somewhat pained by this, as this is an interview question I've gotten, and I clearly annoyed the interviewer by knowing this isn't true, and you can avoid a full sort (which, at least two others have noted).

https://en.wikipedia.org/wiki/Quickselect

joshdavhamtoday at 1:20 AM

I also like this question.

A fun follow up is asking a candidate how to compute the 25th and 75th percentiles or more broadly, the n-th percentile.

show 1 reply
keithnztoday at 2:16 AM

I find this kind of thing too limited and you can't do much with it. I like to take problems from our domain. We work with all kinds of measurement data for agriculture / mining / utilities, I'll usually work through the problem of coming up with an alarm/alert system given a timeseries. It has relatively straight forward programming problems like simple on off threshold alerting and more complex issues like making predictors to decide when to irrigate for example. Depending on the level of the person we can do different things, talk about our domain and some of the problems in that domain. So we can go through specifying things, making design decisions, implementing an interesting aspect (usually not too complex with limited scope), next steps to build the system out, how to validate, logging, etc, feeling out how they'd approach making it production ready basically.

dimviewtoday at 2:13 AM

You can do it a bit faster by not quicksorting the entire array, as you don't really care about the order of the lowest and highest numbers as long as they are not close to the middle.

There is also an approximate algorithm that does not keep all the data in memory at the same time.

show 1 reply
ycui7today at 2:21 AM

there is an algorithm called quick-select. getting median of an array should not require full sort of the whole array, only a partial sort is needed to get the median. quick-select does this.

kccqzytoday at 2:22 AM

After the candidate has finished this, you could then ask them to compute the weighted median. Chances are, the candidate has never heard of this term and yet the term is simple enough that without prior knowledge they can use their intuition to give a definition for this term and implement it. Good candidates can define and implement it for weights that are natural numbers, and better candidates can implement it for any weights that are nonnegative.

Candidates who could implement an O(n) median algorithm but chose to implement an O(n log n) weighted median algorithm might be someone who rote remembered the O(n) algorithm. Truly excellent strong hires can adapt their O(n) algorithm to weighted median too.

ta93754829today at 1:37 AM

I don't know... I've been coding for ~30 years, and I've never had to write code to compute the median so it doesn't seem that useful unless it's somehow relevant to the job

show 2 replies
SeanSullivan86today at 2:04 AM

Huh, feels overly simple to me.

How about something like the beginnings of a spreadsheet engine?

Or.. count the number of distinctly shaped black regions in a bitmap image.

show 1 reply
halaylitoday at 2:35 AM

> Right out the gate: the numbers must be sorted.

if you are not aware of quickselect algorithm.

ukokitoday at 1:22 AM

Only the median (or pair around the median) needs to be sorted, the other numbers can be unsorted :)

show 1 reply
meltynesstoday at 1:45 AM

without thinking about it or looking at the article, this feels rather radixy

tayo42today at 2:00 AM

Id screw this up assuming that sort and pick the middle is to obvious and do something dumb lol

dreamcompilertoday at 1:56 AM

Even better question: Compute the moving median.

Computing a moving average with samples being pumped through an n-element buffer is easy. Doing so for the median requires more thought. It's also very useful e.g. for removing single-sample noise from an audio track, so it's not a meaningless exercise.

show 1 reply
hirvi74today at 1:45 AM

> # Python is pass-by-reference, what are the

> # implications of sorted() vs numbers.sort()?

I thought references were passed by value in languages like Python? I am not particularly fond of Python, so my experience with and knowledge of the language are quite limited. But, I understand what the question is asking: mutation vs. the creation of a new object.

show 1 reply
NooneAtAll3today at 1:51 AM

I thought this was going to be about computing (a+b)/2 avoiding overflows

show 1 reply
harrouettoday at 6:31 AM

...and it can lead to parallel processing discussions for very large arrays

strstrtoday at 1:53 AM

Who does a sort for this D:

show 1 reply
wewewedxfgdftoday at 1:25 AM

Great question! Assuming the job requires calculating a lot of medians.

show 2 replies
dgacmutoday at 2:23 AM

> Right out the gate: the numbers must be sorted.

But they don't. I hope you, as an interviewer, have the grace to learn when one of your interviewees points out your mistake. :-) Median is O(n), not nlogn

jagged-chiseltoday at 1:29 AM

[flagged]