Friday, March 11, 2011

Asurion interview(write a code sample)

THe hiring procedure includes HR interview, Engineer interview, write three codes. I just descript 3 code questions and my answer below.

1. Write a function in JAVA, which input a int array and return a int. The result int  means one of the index of array. The index let the sum of left side equal with right side. The function can find a index. For example, intput a array{-1, 7, 3, 2,4} and result is 2. becasue array[2] = 3. The left side is –1+7 = 6. The right side is 2+4=6.

private int equi(int[] A){
   int left=0;
   int right = 0;
   int k=0;
   if(A.length ==1){
      return 0;
   }
 
   for(int i=1; i<A.length; i++){
       right += A[i]; 
    }
   for(int i=0; i< A.length; i++){
       if(left == right){
            return k;
       }
       left +=A[i];
       right = right-A[i+1];
       k=i+1;
     }
     return -1;
}



2. Write a function in JAVA. The function should find a gap in the binary array……

private int binary_gap(int n){
   Integer integer = new Integer(n);
   String binaryString= integer.toBinaryString(n);
   int length = binaryString.length();
   int gap=0;
   int gap_Max=0;
   for(int i=0; i<length; i++){
      if(binaryString.substring(i, i+1).equals("0")){   
         gap++;
      }else{
         if(gap>gap_Max){
            gap_Max = gap;
         }
         gap=0;
      }
   }
   return gap_Max;
}


3. write a function in Javascript. Input is a string. For example,
’asurion’
0 –> ’asurion’
1 –>’suriona’
2 –>’urionas’
3 –>’rionasu’
4 –>’ionasur’


function cyclic_automorphisms(ori) {
        var temp = ori;
        var automorphisms=0;
        for (i = 1; i < ori.length; i++) {
            temp = ori.substring(i, ori.length) + ori.substring(0, i);
            if (ori.match(temp) == ori) {
                automorphisms = i-1;
            }
        }
        return automorphisms;
 }

No comments:

Post a Comment