Live data from Hacker News

A software engineering interview question I like: computing the median

krisshamloo.com

11–20 of 97 posts

Re: A software engineering interview question I like: computing the median

#11
> # 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.

Re: A software engineering interview question I like: computing the median

#12
post #11

> # 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.

Correct, it’s a common misconception/sloppy wording.

Re: A software engineering interview question I like: computing the median

#15

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

One interview question I like that's simpler and more applicable is to code a function that outputs the frequency count of each word in a string of text. Bonus for outputting the count in most to least order.

Re: A software engineering interview question I like: computing the median

#16

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

That's average or mean. Median is the middle value.

From the article:

> It can lead to some discussion about statistics and why you might prefer a median to a mean in most cases.

My best example for median vs mean is property prices, where very expensive properties will skew the mean (average value) upwards but the median (middle value) will remain about the same.

Re: A software engineering interview question I like: computing the median

#17
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.

Re: A software engineering interview question I like: computing the median

#18

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.

I got that recently, I really didn't like that question.

For the n-th percentile version, the obvious solution is sorting and it takes 10 seconds to get to that point, 5 minutes of implementation with tests. Good. It's all downhill from here.

Then you get hit with the "it's a data stream" and you realize you have to implement a balanced tree on the spot which I wouldn't describe as fun.

You may or may not be able to implement that. I did not. Blabbered something about Rust having sorted B-Trees and I don't think Python has them -- they do not on the standard library.

Then the interviewer leaned heavily on the "reduce memory usage" and I couldn't come up with a solution (no shit it's Ω(n) and he didn't even tell me to go fetch for a randomized algorithm). I later understood he expected the reservoir sampling solution which is basically keeping a representative group of size K that is a good proxy of the whole stream, it goes like this: keep the K first elements, any elements after that replaces any element of our sample at random.

What I did after 10 minutes of weird silence is to assume the data stream follows a normal distribution and computing the P-percentiles by computing the running mean and standard deviation.

I felt frustrated at the end of the interview because it really felt like a big gotcha of either you know the reservoir sampling "leetcode trivia" or you don't.

Re: A software engineering interview question I like: computing the median

#19

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.

I used to translate classic interview questions into not-spoiled-on-the-internet ones by doing this kind of batch to incremental conversion. The count-the-islands one was fun but hard to fit into a 45minute interview.

Eventually most of those started getting spoiled too lol.

Post reply on HN