upvote
Part of what I get from AoC is finding a more optimal solution.

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.

reply
Since m < n by definition, O(n+m) is O(n).

Similarly, there can be no O(m log m) solution, because you have to read every line in general.

reply
Sure, it was shorthand, every algorithm has to be at least O(n) as you have to read in every value at least once. It [Algo (c)] is roughly O(n + (m log m)) but the computation involved generally means that the 'm log m' portion will almost certainly dominate, so the 'O(n)' part is mostly redundant.

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.

reply
O(n + m) is also practically nice to give a general idea of what you mean, without spoiling anything for folks who are still working on it.
reply
Also a big part of the advent of code is to look at the input data and realize that it's not a worst case scenario at all and heavy shortcuts can be taken.
reply
I've had a ton of fun doing this over the years, it's like metagaming the puzzle. I'm not great at it but I love it when I can pull it off.

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.

reply
Indeed. That's what I mean by "some people just want to get the stars and move on".

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

reply
Thanks mate, maybe with practice I'll be able to remember all the arbitrary syntax and finally turn into a real programmer
reply
I never understood the point of arbitrary syntax. Multiplication tables are arbitrary numbers but if your job/hobby is to do long calculations all day I expect you to have them memorised.

I understand now most people don't program much of anything, but sorting a list is really far from arbitrary syntax

reply
Real men write machine code only /s
reply