Amazon Interview Question
Software Engineer in TestsTeam: Kindle
Country: India
Interview Type: In-Person
1)if 60 need to be replaced as 65
for(int i=0;i<array.length;i++)
if(array[i]%10==0) array[i]+=5;
2) if just 0 need to be replaced with 5
for(int i=0;i<array.length;i++)
if(array[i]==0) array[i]+=5;
3) if 0 need to be replaced with 5 in a sorted array
public static void main(String[] args) {
int arr[]= {-2,-2,-1,0,0,0,0,0,0,1,2,3,4};
int index = binarysearch(arr, 0, arr.length-1,0);//get the index of 0
System.out.println(index);
int index2=index-1;
while(true){//replace all the 0's starting from index to upwards
if(arr[index]!=0)
break;
arr[index++] =5;
}
while(true){//replace all the 0's downwards starting from index
if(arr[index2]!=0)
break;
arr[index2--] =5;
}
for(int i:arr)
System.out.print(i+ " ");
}
static int binarysearch(int[] arr, int minSize, int maxSize, int key){
if(minSize ==maxSize) return minSize;
int mid = (maxSize-minSize)/2;
if(arr[mid]==key) return mid;
if(arr[mid]>key)
return binarysearch(arr, minSize, mid-1, key);
else
return binarysearch(arr, mid+1,maxSize, key);
}
#include <stdio.h>
int main(void)
{
int arr[]={1,0,50,55,60,78,100,450,87,0,};
int i;
for(i=0;i<10;i++)
{
if((arr[i] % 10) == 0)
{
arr[i]=arr[i]+5;
}
}
for (i=0;i<10;i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}
int n;
printf("The original list is \n");
for(i=0; i<10; i++)
{ printf("%d, ",a[i]);
}
printf("After conversion the list is \n");
for(i=0; i<10; i++)
{ j=0;
sum=0;
while(a[i]>0)
{ k=a[i]%10;
a[i]=a[i]/10;
if(k==0)
k=k+5;
sum=sum+pow(10,j)*k;
j++;
}
a[i]=sum;
}
for(i=0; i<10; i++)
printf("%d, ",a[i]);
printf("\n");
}
27,1 Bot
If regular expression can be used, following is the code in java..
Please comment if there are any errors.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test {
public static void main(String args[]){
int a[] = {3, 89, 4, 45, 233, 900004, 30, 606, 670, 8097};
Pattern pat = Pattern.compile("0");
for(int i=0; i<a.length; i++){
Matcher mat = pat.matcher(""+a[i]);
a[i] = Integer.parseInt(mat.replaceAll("5"));
System.out.println(a[i]);
}
}
}
the code can replace 0 with 5 in a integer, suppose no pre-zeros in the integer.
- notbad July 26, 2012