## Amazon Interview Question for Software Engineer in Tests

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

hi any one attended the test on that day........and gone thru some rounds of interview.....please post the questions here....

Thanks,
Pavan

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

int a=9999999,sum=10;
while (sum>9)
{
sum=0;
while(a>0)
{
sum=sum+a%10;
a=a/10;
}
a=sum;
}

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

sulabh++;

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

``````int add_digit(int k){
while(k>9){
k = k/10 + k%10
}

return k;

}``````

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

Excellent!!

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

``````//finds the sum of digits of a number
#include<cstdio>

int addDigits(int no) {
int negFlag=0;
int sum=0;
if(no<0) {
negFlag=1;
no=-no;
}
while(no>0) {
sum+=no%10;
no/=10;
}
return sum;
}
int main() {
printf("%d : %d ",567,addDigits(567));
printf("%d : %d ",5,addDigits(5));
printf("%d : %d ",0,addDigits(0));
printf("%d : %d ",-567,addDigits(-567));
}``````

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

@wolverine ur solution doesnot return single digit in the end.

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

Hi, this is simple...

int n = 65; //answer is 2

int sum_till_one_digit = n % 9;

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

There is one trick in this solution -- If it returns 0, answer is actually 9. Need to handle n=0 case separately.
e.g. 999 & 9 == 0, but sum_to_1_digit is actually 9.

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

Hi, this is simple...

int n = 65; //answer is 2

int sum_till_one_digit = n % 9;

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

Hi, this is simple...

int n = 65; //answer is 2

int sum_till_one_digit = n % 9;

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

what abt n = 63?
It should be 9 and not 0

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

(n-1)%9+1

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

@ st0le: your solution works. wow

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

int addnum(int a)
{int temp =a;
int res = 0;
char flag = 1;
do
{
res = res + temp % 10;
temp = temp/10;
if(temp == 0 && res <= 9)
{
flag=0;
}
if(temp == 0 && res > 9)
{
temp=res;
res=0;
}

printf("temp :%d res :%d \n\n",temp,res);

}while(flag);
printf( "final ans :%d \n\n",res);
return res;
}

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

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

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

``````// a recursive solution
unsigned int SumDigits(unsigned int number)
{
if (number >= 0 && number <= 9)
{
return number;
}
return (number % 10) + SumDigits(number / 10);
}

unsigned int SumDigitsTillSingleDigit(unsigned int number)
{
if (number >= 0 && number <= 9)
{
return number;
}
return SumDigitsTillSingleDigit(SumDigits(number));
}``````

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

Can anyone please confirm on this code?

public static long addNumber (long n)
{
long sum = 0;
long asum = 0;
while (n>0)
{
sum = sum + n%10;
n/=10;
}
if (sum > 9 )
sum = addNumber(sum);
return sum;
}

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

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

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

This will not return sum as a single digit.
Ex. if n = 364, the above program would return 13; but, it should actually return 4.

Extending the above code,

int sum = 0;
void sumDigits(int n)
{
while (n>0){
sum = sum + n%10;
n/=10;
}
if(sum/10 > 0)
sumDigits(sum);
else
printf("%d", sum);
}

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

a = 123;
while(a != 0){

num = a%10;
sum = sum + num;
a = a/10;
}
System.out.println("a : "+ sum);

output: 6

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

``````#include<stdio.h>

int main(void) {
int num;
scanf("%d",&num);
if(!num) {
printf("sum of digits : %d\n",num);
return 0;
}
num=num % 9;
printf("sum of digits : %d\n",num?num:9);
return 0;
}``````

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

``````int findSum (int n) {
int sum = 0;
while (n>0) {
sum += n%10;
n /= 10;
}
if (sum > 9)
return findSum (sum);
else
return sum;
}``````

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