So I found this purported interview question(1), that looks something like this
Given an array of length n of integers with unknown range, find in
O(n)
time andO(1)
extra space whether or not it contains any duplicate terms.
There are no additional conditions and restrictions given. Assume that you can modify the original array. If it helps, you can restrict the datatype of the integers to int
s (the original wording was a bit ambiguous) - although try not to use a variable with 2^(2^32)
bits to represent a hash map.
I know there is a solution for a similar problem, where the maximum integer in the array is restricted to n-1
. I am aware that problems like
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Find the maximum repeating number in O(n) time and O(1) extra space
- Algorithm to determine if array contains n…n+m?
exist and either have solutions, or answers saying that it is impossible. However, for 1. and 2. the problems are stronger than this one, and for 3. I'm fairly sure the solution offered there would require the additional n-1
constraint to be adapted for the task here.
So is there any solution to this, or is this problem unsolvable? If so, is there a proof that it is not solvable in O(n)
time and O(1)
extra space?
(1) I say purported - I can't confirm whether or not it is an actual interview question, so I can't confirm that anyone thought it was solvable in the first place.