4 ms·
It doesn’t inspire much confidence in a candidate’s ability to write software of even moderate complexity if their reasoning for using sort instead of a 5-liner
by kevinventullo 6y ago
It doesn’t inspire much confidence in a candidate’s ability to write software of even moderate complexity if their reasoning for using sort instead of a 5-liner was that their 5-liner could contain bugs.
- benrbray 6y agoWriting software of moderate complexity requires delegation. One big way to save dev+testing+debug time is to rely on specialized libraries whose priority is to be fast+correct. If profiling later reveals that sort() is the bottleneck, by all means, optimize away.
- ben-schaaf 6y agoEven worse when you consider most languages have a built-in way to do linear search too, so really it's just turning a fast 1 liner into a more complex slower 1 liner.
- JackFr 6y agoI 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.
- CyberDildonics 6y agoYou 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.
- lixtra 6y agoI 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.
- CyberDildonics 6y agoIt 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
- lixtra 6y agoStandard 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 https://en.cppreference.com/w/cpp/algorithm/max_element
- CyberDildonics 6y agoThat 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.
- kevinventullo 6y ago> 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.
- jancsika 6y ago> 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? :)