Amazon Interview Question for Software Engineer in Tests






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

This is number nine trick. Ans=(Number)%9
Proof I don't know.

- Tulley April 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if (Number)%9 is 0 then ans is 9

- Tulley April 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May not accept numbers with size larger than 12 digits.. But that can be handled..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Summation {
    public static void main(String[] args) throws IOException {
        System.out.println("Enter the Number:");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String snum = in.readLine();
        long num = Integer.parseInt(snum);
        
        while (size(num) != 1) {           
            num = addDigits(num);
        }
        
        System.out.println("Ans:" + num);
    }

    public static long addDigits(long num) {
        long numOfDigits = size(num);
        int startMod = 10;
        long sum = 0;

        for(int i = 0; i < numOfDigits; i++){
            long digit = num%startMod;
            sum = sum + digit;
            num = (int) num/startMod;
        }
        return (sum);
    }

    public static long size(long num) {      
        int digits = 0;
        int step = 1;
        while (step <= num) {
            digits++;
            step = step * 10;
        }
        return (digits);
    }
}

- Jane April 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum(int n)
{
int s;

while (1)
{
s = 0;
while (n > 9)
{
s += n % 10;
n = n /10;
}
n = s;
if (n < 10) break;
}
}

- G April 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
    long no = 312451223999999;
	int sum = 0;
	int flag = 0;

	do{
		if(flag==1){
			no = sum;
			sum = 0;
		}
		while(no/10){
			sum += (no-((no/10)*10));
			no = no/10;
		}
		sum+=no;
		flag = 1;

	}while(sum/10);

	cout << "sum : " << sum;

    return 0;
}

- daisy898 May 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

but seriously? Amazon is asking such simple Q's ?????
Was that a phone intv?

- Anonymous May 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive Solution !

public static int digitsSum(int n)
	{
		if(n<10) return n;
		int sum = 0, d;
		while(n>0) 
		{
			d = n%10;  n = n/10; sum += d;
		}
		return digitsSum(sum);
	}

- RKD May 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above code is correct, except it should simply return sum, instead of recursively calling the function .

- vikram c May 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, if we just return sum for
789 --> 7+8+9 = 24
it will return 24
the answer is
2+4 = 6
So, recursively we have to call the same function again.

- R May 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, I think that digitSum(n) instead of digitSum(sum) is most correct.

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

Oops, my bad. digitSum(sum) is very correct.

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

such a easy question asked by amazon :)

int sumofnumber(int num)
{
int t,sum=0;
while(num/10)
{
t=num;
while(t)
{
sum=sum+t%10;
t=t/10;
}
num=sum;
sum=0;
}
return num;
}

- Anonymous July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int recursiveSum(int num)
{
    if(num % 10 == num)
        return num;

    int lastDigit = num % 10;
    int remainder = num / 10;

    return (lastDigit + recursiveSum(remainder);
}

Time Complexity: O(n).

PS: If there is anything wrong with the code, feel free to correct me ...

- Rahul Arakeri February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to mention the call to this function. It is as follows:

do
{
    int num = recursiveSum(num);
    int x = num / 10;
}while(x!=0);

- Rahul Arakeri February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sumDigits(n):  # assumption: n >= 0
  if n / 10 == 0: return n
  else:
    s = 0
    while n != 0:
      s += n % 10
      n = n / 10
    return sumDigits(s)

- amshali February 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void sumdigits(int num)
        {
            int temp = 0;
            while (num > 0)
            {
                temp = temp + num % 10;
                num = num / 10;
            }
            if (temp > 9)
            {
                sumdigits(temp);
            }
            else
            {
                Console.WriteLine(temp);
            }
        }

- Royal December 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int SumOfDigit(int sum)
{
if(sum<9)
return sum;
string digit = sum + "";
sum = 0;
foreach (char c in digit)
{
sum += (c - 48);
}
return SumOfDigit(sum);

}

- Brave August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SumOfDigits {
public static void main(String[] args) {

int n=123456;
int remainder=n%10;
int sum=0;
while(n!=0){
remainder=n%10;
sum+=remainder;
n=n/10;
if(n==0)
{
n=sum;
if(n/10==0)
break;
sum=0;
}
}
System.out.println("The Sum is:"+sum);
}

}


output: 3

- balraj.dacha December 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SumtoSingle {

public static void main(String[] args) {
int num=810;
int sum=0;
while(true)
{
sum=sum+num%10;
num=num/10;
if(num==0 && sum>9)
{
num=sum;
sum=0;
}
else if(num ==0 && sum<=9)
break;
}
System.out.println(sum);

}

}

- Deepa January 05, 2015 | Flag Reply


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