Amazon Interview Report
- 0of 0 votes
AnswersHow to check a whether a very big number n^n = k
- banerjees36 June 22, 2012 in India| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Systems Design Engineer Algorithm Data Structures - 0of 0 votes
AnswersHow to most efficiently find duplicates/commons in two sorted arrays of integers. No extra space should be used. My answer as below
- banerjees36 June 22, 2012 in Indiapublic class Duplicate { //private HashSet<Integer> duplicates = new HashSet<Integer>(); /** * @param args */ public static void main(String[] args) { int a[] = {0,2,2,4,6,8,8,9,9,10}; int b[] = {0,3,3,4,8,8,10,12}; new Duplicate().mergeAndCheck(a, b); } private void mergeAndCheck(int[] arr1, int[] arr2){ int len1 = arr1.length; int len2 = arr2.length; int pointArr1 = 0; int pointArr2 = 0; while((pointArr1<len1)&&(pointArr2<len2)){ if(arr1[pointArr1]<=arr2[pointArr2]){ if((arr1[pointArr1]==arr2[pointArr2])){ if(pointArr1==0){ System.out.print(" "+arr1[pointArr1]); }else if(arr1[pointArr1]!=arr1[pointArr1-1]){ System.out.print(" "+arr1[pointArr1]); } //duplicates.add(arr1[pointArr1]); } pointArr1++; }else{ pointArr2++; } } /*for(Integer iNT:duplicates){ //System.out.print(" "+iNT); }*/ } }
| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer - -1of 1 vote
AnswersWithout using extra space place all zeroes to left and 1's to right from an array of Zero's and 1's as below
011001 ans. 000111
- banerjees36 June 22, 2012 in Indiapublic class Zeros_ones_New { /** * @param args */ public static void main(String[] args) { int[] arrZerosOnes = { 0,1,0,1,0,1,1 }; int len = arrZerosOnes.length; int firstOne = -1; int lastZero = len; for (int i = 0; i < len; i++) { if (arrZerosOnes[i] == 1) { firstOne = i; break; } } for (int u = len - 1; u > -1; u--) { if (arrZerosOnes[u] == 0) { lastZero = u; break; } } if (firstOne > -1 && lastZero<len) { while (firstOne<lastZero) { for (int j = firstOne; j < lastZero; j++) { if (((j + 1) <= lastZero) && (j == firstOne) && (arrZerosOnes[j + 1] == 0)) { arrZerosOnes[j] = 0; arrZerosOnes[j + 1] = 1; firstOne++; /*if ((j + 1) == lastZero) { break; }*/ } else if (((j + 1) <= lastZero) && (arrZerosOnes[j] == 1) && (arrZerosOnes[j + 1] == 0)) { arrZerosOnes[j] = 0; arrZerosOnes[j + 1] = 1; if ((j + 1) == lastZero) { lastZero--; } } } } } for (int t = 0; t < len; t++) { System.out.print(arrZerosOnes[t]); } } }
| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer