News for hackers. Stuff that matters.
Re:Optimizations on Linear Search (2016) by JackFr on Saturday October 03, 2020 @12:35PM (#24673081)
I sometimes ask "In the language of your choice write a function to find the maximum of a list of integers" as an interview question. I often have to typically follow it by "Really, there's no subtle trick, just find the max...". The number of people who use a 'sort' and take the first element is definitely non-zero. It's also astonishing the number of buggy implementations there are. (Well in fairness there is one buggy implementation which everyone who gets it wrong typically makes -- initially setting the max to 0)

I'm not sure if this is an argument for or against coding exercises in interviews -- it was just a surprising experience for me.

Re:Optimizations on Linear Search (2016) by CyberDildonics on Saturday October 03, 2020 @01:39PM (#24673507)
You have never had anyone initialize the max to MIN_INTEGER or something like that? I have made similar mistakes, but I've had to write these functions before, so I know better.
Re:Optimizations on Linear Search (2016) by lixtra on Saturday October 03, 2020 @02:27PM (#24673798)
I couldn’t remember such a thing as MIN_INTEGER and what if someone changes the data type?

Why not assume the first element is the max and start your search from the second. Ah, dam. Now you have to check that your vector has at least one element.

It ain’t easy.

Re:Optimizations on Linear Search (2016) by kevinventullo on Sunday October 04, 2020 @09:04AM (#24678737)
> Now you have to check that your vector has at least one element.

Which may lead the candidate to ask the interviewer “What should be the behavior on an empty vector?” Returning MIN_INT or throwing an exception may both be reasonable depending on the application, but I would consider it good signal if the candidate at least acknowledges that this is an edge case.

Re:Optimizations on Linear Search (2016) by CyberDildonics on Saturday October 03, 2020 @05:49PM (#24674926)
It is pretty easy, I would expect that at least some people would be able to do it. MIN_INTEGER was an example, it should be obvious you want the minimum for whatever type you are using. Standard C++ has std::numeric_limits<T>::lowest
Re:Optimizations on Linear Search (2016) by lixtra on Saturday October 03, 2020 @06:45PM (#24675233)
Standard C++ also has std::max_element [1] (and accidentally the possible implementation looks surprisingly familiar). Not that I know much of C++.

[1] https://en.cppreference.com/w/cpp/algorithm/max_element

Re:Optimizations on Linear Search (2016) by CyberDildonics on Sunday October 04, 2020 @11:22AM (#24679668)
That returns an iterator the max element, not the value. I'm not sure which part makes this so difficult that everyone fails on the first try.
Re:Optimizations on Linear Search (2016) by jancsika on Saturday October 03, 2020 @07:02PM (#24675314)
> I sometimes ask "In the language of your choice write a function to find the maximum of a list of integers" as an interview question.

And you don't find it important at all to specify whether the code will be in the hot path or not?

Let me ask you-- real quick question here-- how much time would you say you spend each week dealing with these TPS reports? :)