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