Pergunta de entrevista da empresa LinkedIn
They asked to write a function that takes a string and check if it is a number. The second question was given a sorted array that has been rotated, find a number in that array {6,7,1,2,3,4,5}
Respostas da entrevista
no you can't just do that. You have to check for decimal numbers, negative numbers.
static int find (int[] a, int i)
{
int j;
for (j=0; j a[j+1]) {
break;
}
}
if (j == a.length) {
return - 1;
}
return biSearch(a, j, a.length - 1, i);
}
static int biSearch(int[] a, int b, int e, int i)
{
int index = (b + e) / 2;
if (i == a[index]) {
return index;
}
if (i e){
return -1;
}
else {
return biSearch(a, index + 1, e, i);
}
}
Its simple, once you realize that the pivot point causes all the subsequent numbers to be < all the previous.
Than for any block, if first number in block is < last number in block, that block does not contain pivot.
int findPivot( int *ar, int lo, int hi )
{
int mid = (lo+hi)/2;
int piv;
if( lo == hi )
return lo ;
if( ar[lo] <= ar[hi] )
return -1 ;
piv = findPivot( ar, lo, mid ) ;
if( piv != -1 )
return piv ;
return findPivot( ar, mid, hi ) ;
}
For this Question
They asked to write a function that takes a string and check if it is a number.
public static boolean isNumber(String inputStr)
{
char in [] = inputStr.toCharArray();
for(int i = 0;i 57)
{
return false;
}
}
return true;
}