Recently I had an interview, where they asked me a "searching" question.
The question was:
Assume there is an array of (positive) integers, of which each element is either
+1
or-1
compared to its adjacent elements.Example:
array = [4,5,6,5,4,3,2,3,4,5,6,7,8];
Now search for
7
and return its position.
I gave this answer:
Store the values in a temporary array, sort them, and then apply binary search.
If the element is found, return its position in the temporary array.
(If the number is occurring twice then return its first occurrence)
But, they didn't seem to be satisfied with this answer.
What is the right answer?