Amazon Interview Question for Software Engineer / Developers






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

int Sum(int n){

int count = 0;
int r1 = n%10;
n = n/10;
while (n>9){
int r = n%10;
n = n/10;
r1 = r+r1;

if( r1 >10 ){
count++;

r1 = r1-10;
}

if(count >9){
count =1;
}
}
cout<<count<<endl;
r1 = r1+n;
if( r1 >10 ){
count++;
r1 = r1 - 10;
}
if(count >9){
count =1;
}

return (count+r1);
}

- tito March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindSum(int num){
//If num is single digit
        int sum = 0;
	if(num / 10 == 0)
		return num;
	else
	{
		while(num > 1){
		   	sum = sum + num % 10;
			num = num / 10;
		 }
		//If sum is not single digit
		if( sum / 10 != 0)
			sum = FindSum(sum);
	}
	
	return sum;
}
int main( )
{	
	cout << FindSum(9999) << endl;
	return 0;
}

- sachin323 March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming non-negative numbers --
we can take advantage of the fact that

singleSum(994) = singleSum(9 + singleSum(94))

therefore,

int singleSum(int num) {
     int n = num;
     int sum = 0;
     int mod = 0;
     while(n) {
        mod = n % 10;
        n = n / 10;
        sum += mod;
        if(sum > 9)
            sum -= 9;
     }
     return sum;
}

- A March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Modifying Sachinsaner's code to make it work (Just added a >= for num>1)

int FindSum(int num){
//If num is single digit
int sum = 0;
if(num / 10 == 0)
return num;
else
{
while(num > 1){
sum = sum + num % 10;
num = num / 10;
}
//If sum is not single digit
if( sum / 10 != 0)
sum = FindSum(sum);
}

return sum;
}
int main( )
{
cout << FindSum(9999) << endl;
return 0;
}

- AJ April 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SumofDigits {

    public static int sumofdigits(int inputnum)
    {
        int quot = inputnum;
        int rem = 0;
        while(quot > 0)
        {
            rem = rem + (quot % 10);
            quot = quot/10;
        }
        if(rem/10 > 0)
        {
            rem = sumofdigits(rem);
        }
        return rem;
    }

    public static void main(String[] args)
    {
        int result = 698239623; //input number
        result = SumofDigits.sumofdigits(result);
        System.out.println(result);
    }
}

- SROD April 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Sum(int n)
{
while (n > 9)
{
int sum = 0;
do
{
sum += n % 10;
n /= 10;
} while (n > 0);
n = sum;
}

return n;
}

- coder April 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Integer findNumber(Integer sum,Integer i){

if(i == 0 && sum > 10)
return findNumber(0,sum);
else if(sum<10 && i == 0)
return sum;
else
return findNumber(sum+(i%10),(i/10));
}

- Woggle April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int calculate(int num) {
while (num > 10) {
int curr_num = num;
num=0;
while (curr_num > 0) {
num += curr_num % 10;
curr_num = curr_num /10;
}
}
return num;
}

- ozanokanavci April 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int out(int in){
int val;
if(int(in/10)==0)
return(in);
val=in%10+out(int(in/10));
return(int(val/10)==0?val:out(val));
}

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

int FindSum(int num)
{
  int sum = 0
  while(num > 9)
  {
    sum = sum + n%10;
    num = num/10;
    if(num < 10)
    {
      num += sum; sum = 0;
    }
  } 
}

- swapnesh April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

forgot to return number...

int FindSum(int num)
   {  
     int sum = 0  
     while(num > 9)  
     {    
        sum = sum + n%10;    
        num = num/10;    
        if(num < 10)    
        {      
          num += sum; 
          sum = 0;    
        }  
      }
      return num;
    }

- Anonymous April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You must be kidding around here.

number % 9

987 % 9 = 6 = 9 + 8 + 7 = 2 + 4 = 6

- ankushbindlish May 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain why this works?

- Anonymous May 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't work for multiples of 9 (9, 18, 27, ....)

- PA June 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

digit 9 has a secial property that when added to another digit, single digit some would be the other digit. When multiplied by other digit the resulting single sum woulb be 9.

using above observations, solution can be

if(num%9 == 0)
return 9;
else
return (num%9);

- hello2008 June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good thing to know. great!

- neo February 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

- hello2008
nice reply ....good job !

- surya April 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

9 has a property, when added to a digit ...becomes the digit e.g. 9+7=16 = 1+6=7
when multiplied wid digit, resulting single digit sum is 9 e.g. 9*7 =63 =6+3=9

Moreover if number>9 sum cant be 0

if(num>9)
{
if(num%9==0)
return 9;
else
return (num%9);
}
else
return num;

- hello2008 June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#include<stdio.h>
int main()
{
int r=0,n=0,sum=0;
scanf("%d",&n);
while(n)
{
r=n%10;
sum=sum+r;
if(sum>9)
sum=sum-9;
n=n/10;
}
printf("%d",sum);
return 0;
}
}}

- jaggu September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c" line="1" title="CodeMonkey59881" class="run-this">#include<stdio.h>
int main()
{
int r=0,n=0,sum=0;
scanf("%d",&n);
while(n)
{
r=n%10;
sum=sum+r;
if(sum>9)
sum=sum-9;
n=n/10;
}
printf("%d",sum);
return 0;
}
</pre><pre title="CodeMonkey59881" input="yes">123
</pre>

- Anonymous September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findsum(int n)
{
int sum=0;
while(n>9)
{
while(n)
{
sum=sum+n%10;
n=n/10;
}
n=sum;
sum=0;

}
return n;
}

- geeks July 15, 2011 | 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