An Interview Question Too Many Developers Get Wrong
openmymind.net
openmymind.net
* How many times will you look for each number?
* How big is the original data?
* How sorted is the original data?
* What kind of access patterns do you have for lookups? (i.e. are the lookups themselves sorted)
* Are there perhaps special space constraints to consider that may have priority over time constraints?
* Where/how is the data stored/how is it backed? (perhaps you're reading from tape media or other non-random-access-optimized storage)
* Is the data a set or bag? (duplicate values allowed, do you care which one is returned, etc)
What exactly are the tradeoffs of adding an index?
If the array of numbers is mutable, the index also needs to be a modifiable structure (like a tree).
>What factors influence which approach, between a linear scan and a sort+binary search, you should pick for an unsorted array?
"A" search implies once. Linear will always be better, since sorting implies visiting every element at least once, followed by a lookup, where linear is at most once. You can of course come up with data structures and contents where this will not be true, but if you're asking for all solutions to everything, the only possible answer would be "it depends".
As a developer, understanding why and coming to an efficient solution (in terms of development hours and customer satisfaction) is about problem decomposition and problem solving skills, not being an algorithm expert.
The easiest way to paint yourself into a corner on a technical interview question - especially a trick one - is to answer it without asking clarifying questions about boundary conditions, usage patterns, data set sizes, target hardware, etc.
It's not a trick question, it's as much a part of quality software development to ask the right questions as it is to start hacking on the keyboard. The right answer to any interview question starts with "it depends, ..."
Building any alternative data structure from an unsorted array will require O(n) time. If you only need to find something once, then a O(n) linear scan time is optimal.
Do you need to implement sorting/binary searching or find libraries, or do you have libraries handy?
I guess the "correct" answer is "lots of things could affect the choice, tell me more about the context".
All of the options expressed here will usually be built in to the language, there is no extra implementation cost at all. Pick a vector, sorted vector, set, map or hash-map.
Knowing and picking what is usually correct will not take more than a few seconds of extra thought.
Your attitude leads to "generally slow" programs that are the hardest to make faster.
Really? Even if they have proper encapsulation, separation of concerns, etc. (even unintentionally)? "Refactor the function or method later if necessary" is not the worst way to write software, and in fact I would say it's the dominant technique even when initial attempts are optimized.
Be very wary of questions that may just be a mechanism for demonstrating how smart your team is to candidates.
It raises two questions: in what way does the terminology serve the company's business goals, and when was the last time the company had to refer to Big-O in order to solve a problem?
I'd argue you should have good test coverage and a well thought out API such that you can easily swap out the searching algorithm as needed to meet your needs without worries of breaking the component or things that depend on it. What if the function is handed a pointer to the array, and the caller doesn't want the data sorted?
Beyond that, I'd strongly argue the answer is: whatever can get me the answer with the most readable and simplest code. If this part of the program later proves to be a bottleneck, then consider different algorithms. Which again, good test coverage plays a roll in.
If you want the job, do whatever it is you think you will impress the employer, unless whatever that would take convinces you you don't want the job.
I disagree because I've been in interviews where a question like this, and all questions asked for that matter, have a testing expectation to them. If a developer answers this question without considering test coverage and what that means, some companies see that as a sign of a dev who doesn't appreciate tests or have a full grasp of the entire lifecycle of development. "Trick questions" with hidden undertones are very common.
Or is it a practicality question? I do not want to hire a dev who is concerned with squeezing every drop of performance out of function calls that make up 0.001% of the app's runtime.
Is finding out the candidate is aware that sort+binary search is O(nlogn) versus O(n) of linear search really enabling you to find a quality candidate? Maybe, but probably not. We don't ask questions like this at all anymore. The bulk of our interview process is sitting down with the candidate and writing a small program with them, end to end. Not perfect either, but far more effective than ambiguous questions.
If you want me to figure out if searching an array is the best option in the first place, then ask me that question. It is an important skill to ask questions - especially when you have to figure out what customers really need versus what they have requested - but expecting that a simple interview question with extremely narrow scope like this makes me question if an array is the correct choice is ridiculous.
It's a bit like when your math test asks you to calculate some annual interests for you savings account and you start to question if bringing money to a bank is the best option in the first place.
For example, suppose the array is built when the program starts, but you know the need to search it will not arise until significantly later, and you also know that the program will have significant idle time in between. You could then use that idle time sort the array.
It seems like there are some serious grammar issues with this question. If the interviewer cannot clearly ask a question, that may explain why many interviewees "get it wrong."
But they never said that numbers were the only thing in the array.
edit: Puzzles like this: http://kith.org/logos/things/sitpuz/situations.html
Either way, questions of this nature are intended to filter out people who are not interested in getting to the root of a problem, and solving that problem practically.
The interviewee should be looking to determine constraints, limitations, expectations, requirements, use cases, problem scale.
The interview for any job should be looking for someone looking for those things.
In what way would any job that requires critical thinking be considered boring?
And yet it's asked.