This is an interview question.
Say you have a function foo()
which returns some integer.
You need to write an algorithm that does the following:
(1). Call foo()
in a loop, till the returned value of foo()
is -1.
(2). Returns true if during the loop there were two calls of foo()
which returned the same value.
This is what I thought to do, please tell me if this is efficient enough:
(1). Initialize a hash map , which maps an integer to the number of times we got it in the loop
(2). Call foo()
in a loop till the returned value of foo()
is -1, while updating the hash map accordingly.
(3). Check if there is a key in the hash map which is mapped to a value greater than 1.