by alexfoo · 2d ago · 0 comments
Some people stick to just getting the stars at whatever cost, others look at the challenge in a different way.
With this problem there's:
a) Plenty of solutions that are O(n^2) and worse
b) An O(n log n) solution where n is the number of lines of the input
c) An O(m log m) solution where m is the number of unique values in the input
d) An O(n+m) solution where n is the number of lines in the input and m is the number of unique values in the input
I could tell a lot about someone's real world programming ability if I brought this up in an interview, especially with follow-up questions such as:
1) What if the input was so large it could not be held in memory all at once?
2) What if the input was so large that even a list of unique values could not be held in memory?
etc...
[EDIT]
If you can just point an LLM at it and get the answer without providing anything useful yourself then you've got nothing to differentiate you from every other candidate that can do the same. I'm not looking for people with multiple PhD's but I look for people who know enough to know when the LLM is spouting nonsense or is suggesting something suboptimal that will fail when scaled up.
Interviewing has become more fun because of this. We tailor our interviews so the AI assisted portion gives the candidate enough rope for the AI to paint themselves into a corner if not careful, and then the non-AI portion weeds out the people who don't have (or have lost) the ability to think for themselves.
Similarly, there can be no O(m log m) solution, because you have to read every line in general.
A n = 2 billion line input with a valid deterministic solution could have m = 1 billion unique numbers (which means 'm log m' likely dominates), or it could have as few as m = 6 unique numbers in which case the O(n) part will likely dominate. Basic O() notation is too coarse a measure to capture this nuance.
The O(n log n) solution seems to me like the trivial/straightforward one: count occurrences, then sort the pairs based on the count. Not sure how to get the O(n^2) without trying specifically to do so (like implementing a sort algorithm from scratch and making it quadratic)
Taking a deeper look can provide some very useful insights and a better understanding of the more general problem which often translates into real world scenarios in companies that operate at scale.
Optimising something that sorts 20 values is often pointless unless it really lies on a hot path and is called very frequently. It's more about when something that was written to sort ~100 values suddenly starts to become a bottleneck as the service grows and grows unexpectedly and is suddenly needing to sort 100k values, then 100M values, etc.
Taking this particular problem as an example, I've had candidates who run the complete spectrum from claiming it's impossible to find the top 3 and bottom 3 values without sorting the entire list of frequency counts (one even claimed we'd have to sort the raw input list for any chance of getting an answer), to the candidate that says "...once you have the frequency counts then finding the final answer is just a pair of max-heaps of size 3, but this depends on whether you can hold the set of frequency counts in memory or not, if not we'd have to take a different approach. Also, max-heap might seem overkill, if you wanted highest and lowest you wouldn't use a pair of max-heaps of size 1, but 3 is about the cutoff where manual tracking of the top/bottom values becomes tricky, and using a heap makes it trivial to move to 4, 5 or any other value if that becomes a need in the future".
(To be clear, I've never asked/discussed this specific question in an interview, I'm just translating responses for a similar problem.)
I don't think I'd hire the candidate who makes iterating a list of numbers and storing the highest/lowest values so complicated either. especially since they hand wave over the actually interesting part about what to do if you can't store the set of frequencies in memory. especially since in many languages instantiating a heap structure might be less performant than first_highest, second_highest, third_highest etc.
I got Claude to implement the function for highest and lowest three values (prompt below) and it did a reasonable job. The function is 46 lines long with 5 lines of comments and 2 blank lines, so 39 lines of actual code (formatted as per gofmt standards).
Telling it to keep track of the highest 4 and lowest 4 and it just extended the function with another set of conditionals. adding another 6 lines of code, and another level of indentation.
45 lines of code within a single function is teetering on the edge of guidelines for maintainability/readability.
Asked it to extend it to highest 5 and lowest 5 and it has extended the function again. Another 6 lines of code. We now have code that is indented 6 levels and a couple of lines are 91 characters wide.
I'm wondering when it's going to switch between adding another layer of conditions to going "this is better refactored as X"
6 highest/lowest and it refactored something to keep the line lengths down but still sticking with its original plan.
Finally at 7 it decides to change things:
" The number has gone up by one each time, so this time I'll also write a version that takes the count as a setting and benchmark it against the hand-written seven-value function. That will show whether keeping the hand-written approach is still worth it.
...
The any-size version came out almost three times faster, which wasn't what I expected. I'll check whether it also beats the hand-written version at smaller sizes.
...
TopBottomK stays at about the same speed whatever the count, while the hand-written version slows down with each value you add. "
The generic TopBottomK was also faster than the hand-written version that just tracked the highest and lowest 3 values.
It's implemented this in 31 lines (excluding comments and blank lines) that out-performs the original "hand-written" versions (as Claude describes them), despite the fact that the original prompt suggested that performance was important.
The generic solution is faster than the count specific solutions, in fewer lines, and handles arbitrary length result sets.
Back to the original point, I'm looking for the candidate that can discuss these kinds of nuances, not come to me with preconceived ideas of "this will be the fastest" or at least qualify assertions with "I would assume that algorithm X should be fastest, but bench-marking the various options would give us the data we need to decide". I don't expect candidates to know definitive answers to these types of questions, I'm probing for their ability to acknowledge where their knowledge starts to get fuzzy. Candidates that are truly open and candid like this are becoming rarer in my experience.
Prompt: write a golang method to loop through a slice of integers of arbitrary size keeping track of the highest three values and the lowest three values. This code is on the hot path of a process so performance is important, keep it as close to O(n) as possible.
I also have used Benchee (Elixir benchmarking library) to run comparisons between naive/bad algorithms that I wrote initially and then the "correct" method after - always kind of edifying to see the difference between log n and n ^2 or whatever actually visible in the benchmarks.