4 ms·
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 fr
by lixtra 6y ago
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.
- 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.