As usual, please be brutally honest with your opinion of my code, and how you would judge it if I were to code this at a top-tier tech company for an interview(think Google, MSFT, Facebook, etc). My algorithm is has a worst time complexity of O(n).
Question: Given an unsorted integer array, find the first missing positive integer. For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.*
My code(coded in 22 minutes, with all test cases passed, devised initial algorithm in 2-5 minutes)
public int firstMissingPositive(int[] A) {
int minVal=0;
if(A.length==0){
return 1;
}
Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
for(int i : A){
if(i<minVal && i>0){
minVal = i;
}
myTable.put(i, i);
}
for(int i=0; i<=A.length; i++){
if( (!myTable.containsKey(minVal-1)) && minVal-1>0){
return minVal-1;
}
else if(!myTable.containsKey(minVal+1)){
return minVal+1;
}
minVal++;
}
return minVal;
}