Live data from Hacker News

A software engineering interview question I like: computing the median

krisshamloo.com

31–40 of 97 posts

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

#31
post #14

Who does a sort for this D:

You need the middle value. You cannot be certain what is the middle value without a sorted array, unless I am mistaken.

You can solve it with two heaps, which don't need to maintain a complete order. Or selection algorithms, as in the sibling comment (asymptotically better).

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

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

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

#33
post #14

Who does a sort for this D:

You need the middle value. You cannot be certain what is the middle value without a sorted array, unless I am mistaken.

You can use Quickselect (https://en.wikipedia.org/wiki/Quickselect) or Floyd-Rivest (https://en.wikipedia.org/wiki/Floyd%E2%80%93Rivest_algorithm)

Quickselect is fairly simple to understand if you already understand Quicksort. You use use a binary division but you avoid sorting sections where the order doesn't matter.

Let's start with a 7 element array

    [ 2, 4, 7, 5, 3, 6, 1 ]
We pivot on the mid-point (5) so that values less than end before it in the array and numbers larger end up after it

    [ 2, 4, 3, 1, 5, 7, 6 ]
Since 5 is now at an index greater than the midpoint, you know the median must be less than 5, so you don't care that 7 and 6 aren't sorted.

We pivot the first partition (first 4 elements) on 3 and get

   [ 2, 1, 3, 4, 5, 7, 6 ]
We don't care that 2 and 1 are unsorted, because we know that the median is > 3 (3 is at index #2 and we want index #3), so the median must be 4

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

#34
post #25

Earlier quoted context omitted.

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…

Literally the second I read "it's a data stream" I knew the answer was going to be reservoir sampling. RS is really interesting to me. many people you talk to can realize you can compute the mean of a data stream ( https://www.geeksforgeeks.org/web-tech/expression-for-mean-a... ) without knowing the exact formulation. And it's not far from that to think of a sampling strategy to decide if a new sample should go into…

I'm pretty confident I would've been able to come up to the solution in better circumstances, maybe even without hints.

But it was clear in this case that the interviewer just took a question from the company's bank of questions and was alt-tabbed for half the interview, I have felt the energy early and I was also half-checked out.

I'm aware I'm saying this post factum, but I had a very fun first interview with that company and matched well with the first interviewer so my expectations were high, and then I got hit by the big tech style interview when it was an early stage startup.

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

#35

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.

So for about 10 years my main interview question was:

"Write me a function in any language of your choosing, that takes an array of integers and returns the sum."

I loved it. Here is why:

1. I'd get to see them write code, in a low pressure way, but they'd have to write something

2. A shocking number of people would struggle to write the code. That was my signal to end the interview early.

3. I'd get to ask "So tell me how it works" and they'd sometimes look at me like I'm a moron, but others would be respectful and kind, and that would tell me how they'd answer other people who ask questions they felt had obvious answers.

4. I'd ask "what could go wrong at runtime?" - this would be where most people got surprised by their own responses, but it was a fun conversation to have about a seemingly simple function.

5. I'd ask how they would fix any potential runtime exceptions or potential undesirable behaviours

6. I'd try break it, and ask how they would handle that (if i could, i often could)

7. If we got this far, then we could move onto other questions and they're warmed up and generally feeling safe about how the conversation would go. I'd like to switch from coding into data structure related questions normally.

I hate high pressure coding interviews, also, who the hell doesn't just sit there and Google / LLM the answer anyway. The real question I want to know is "How curious are you? Do you want to learn? What kind of person are you? Will I enjoy working with you when things get hard". That's hard to figure out, but you're not going to do that if you just try stump someone in an interview. I think it's on the interviewer to find a way to ask questions that are revealing and accessible in an interview environment...and frankly, I think you get more out of it if you make the effort to keep it simple.

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

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

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

#39

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

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

A binary search[0] of a sorted collection requires the median of each region being considered for each iteration.

0 - https://en.wikipedia.org/wiki/Binary_search

Post reply on HN