|
|
|
|
|
by slopinthebag
2 days ago
|
|
> "...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". 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.