Amazon Interview Question for Software Engineer in Tests


Team: Kindle
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

the code can replace 0 with 5 in a integer, suppose no pre-zeros in the integer.

int replace0to5(int n){
        if (n == 0) return 5;
        int res = n;
        int added = 5;
       while (n > 0) {
           if (n % 10 == 0) res += added;
           added *= 10;
           n /= 10;
       }
      return res;
   }

- notbad July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

one mistake

if(n%10 == 0) .. 
you have mentioned as
 if(n%10==10)

- cobra July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cobra thx, have corrected it.

- notbad July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

means we need to convert 60 to 65 or.. only the whole number '0' to '5'

- cobra July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

check ideone.com/S0aJL
it converts 0 to 5 , 60 to 65, 1000 to 1555

- cobra July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You means like the below ex:
i/p: 1 0 3 0
o/p: 1 5 3 5

Correct me if I am wrong

- Begineer July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

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);
	}

- pratap July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code have some flaw . it change 1000 to 1005 but not 1555..

- cobra July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- Avi July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its not correct according to you 100 is made as just 105 but it to be 155

- Siva Krishna July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<array.length;i++)
if(array[i]%10==0) array[i]+=5*pow(10,i);

- Abhishek Dutta July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

testing if a[i]%10==0 and adding 5 is insufficient. What happens if a[i] is 100? Then you would replace it with 105, and you would still have a 0.

- pws July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class intToString{
public static void main(String[] args){
int i;
int array[]={1,0,50,55,60,78,100,450,87,0,};
String s;
for(i=0;i<10;i++)
{
s=""+array[i];
String newStr =s.replaceAll("0","5");
array[i]=Integer.parseInt(newStr);
//System.out.println(array[i]);
}
}
}

- Ankush July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

main()
{
  int n,arr[10],i,fact,k;
  printf("Enter the limit:\n");
  scanf("%d",&n);

  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);

  for(i=0;i<n;i++)
    {
      for(k=arr[i],fact = 1;k>0;k/=10,fact*=10)
	{
	  if(k%10==0)
	    arr[i]+=5*fact;
	}
    }

  for(i=0;i<n;i++)
    printf("%d\n",arr[i]);


}

- Angel August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void replace (int array1[])
{
int i = 0,j=10,k=5,temp;
for(;i<SIZE;i++)
{
if(array1[i]==0)
{
array1[i] = 5;
}
else
{
j=10;
temp=array1[i];
while(temp>9)
{
if((temp%10)==0)
{
array1[i]= array1[i]+j/2;

}
temp =temp/10;
j=j*10;
}
}
}
}

- Meena August 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- pratap August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you change the int to a string and throw each char into an array.
Put 1004 into an array like: [1][0][0][4] and then go through the array and change the 0's to 5's and then re-create the number from a string. [1][5][5][4] = 1554.

- ShaunW August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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]);
		}
		
		
	}
}

- Praveen October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a simple Python code.

def replace0to5 (num):
m = 10
n = 1
d = len(str(num))

for i in xrange(d):
if ((num % m) / n == 0):
num = num + n * 5
m = m * 10
n = n * 10

return num

- Anonymous November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ankush's code worked for me

- Anonymous December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple O(n) solution in ruby

# O(n)
def replace_zeroes zero_arr = []
  0.upto(zero_arr.size) {|i| zero_arr[i] += 5 if zero_arr[i] == 0}
  zero_arr
end

- VinceBarresi September 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class ArrEx{
        public static void main(String[] arg)
        {
                int arr[] = {1,4,0,2,5,0,6};
                for(int i=0; i<arr.length; i++)
                {
                        if(arr[i]==0)
                        {
                                arr[i]=5;
                        }
                }

                for(int x:arr)
                {
                        System.out.println(x);
                }
        }
}

- Anonymous July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class ArrEx{
        public static void main(String[] arg)
        {
                int arr[] = {1,4,0,2,5,0,6};
                for(int i=0; i<arr.length; i++)
                {
                        if(arr[i]==0)
                        {
                                arr[i]=5;
                        }
                }

                for(int x:arr)
                {
                        System.out.println(x);
                }
        }
}

- puja July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you have a 0! in an integer array? okay I am assuming it's an int array but that's what the question seems to say.

- Anonymous July 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for(int i=0;i<array.length;i++)
if(array[i]%10==0) array[i]+=5*pow(10,i);

- Abhishek Dutta July 28, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More