Amazon Interview Question for Software Engineer / Developers






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

#include<stdio.h>
int func(int a,int cnt)
{
int ans=0;
if(cnt==0)
return 0;
ans=a+func(a,--cnt);
return ans;
}
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",func(a,b));
}

- naive October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

above code fails in case of negative numbers.

- jai October 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
private static int recMultiplyHelper(int x, int y)
{
if (y == 1)
return x;
else
return x + recMultiply(x, y - 1);
}

public static int Multiply(int x, int y)
{
if (x == 0 || y == 0)
return 0;
bool isAnswerNegative = false;
if (x < 0 && y > 0 || x > 0 && y < 0)
isAnswerNegative = true;
if (x < 0)
{
x = x - x - x;
}
if (y < 0)
{
y = y - y - y;
}
int answer = recMultiplyHelper(x, y);
if (isAnswerNegative)
{
answer = answer - answer - answer;
}
return answer;
}
}

- new.learner67 October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

instead of using something like x - x - x, u can use -(x)

- rs October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

-(x) is sort of multiplication operation -1*x and violates a constriant

- new.learner67 October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

use 0-x

- bang October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is another version

int multiply(int a, int b) {
	if (a == 0 || b == 0) {
		return 0;
	}
	a += multiply(a, abs(b) - 1);
	return (b < 0) ? -1 * a : a;
}

- Anonymous October 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My Bad... used a multiplication operator.

updated version

int multiply(int a, int b) {
	if (a == 0 || b == 0) {
		return 0;
	}
	a += multiply(a, abs(b) - 1);
	return (b < 0) ? (0 - a) : a;
}

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

I think it wont work if a is -ve.

- anony December 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

nice one !

- mr October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just to make sure you are fixing one Integer and parsing another into function using recursion? Right?

- mr October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int multiply(int a, int b)
{
int ans=0;

if(b==0)
{
return 0;
}

if(b>0)
{
a += multiply(a,--b);
}
else if(b<0) {
a = -a;
a -= multiply(a, ++b);

}
return a;
}

- Coder November 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int multiply(int a, int b)
{
if (a == 0 || b == 0)
{
return 0;
}
if (abs(b) > abs(a))
{
return multiply(b, a);
}

return (b>0)? a+multiply(a, --b) : multiple(a, ++b) -a;
}

Reply to Comment

- durbin May 13, 2010 | 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