Amazon Interview Question for Software Engineer / Developers






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

int divide(int numerator, int denomenator)
{
int count = -1;


do
{
count++;
numerator = numerator - denomenator;
} while(numerator>0)
return count;
}

- abcd November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code gives, 2/1 = 1, 10/2 = 4
maybe count = 0 is a better idea?
Worst case complexity : O(numerator/denominator)

- Synonymous November 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do we have to give output in integer or decimal points as well. Integer its straight forward subtraction

- JoshMachine November 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int count=0;
while(num>=den)
{
num=num-den;
count++;
}
return count;

- anonymous November 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Math.pow(Math.E, Math.log(num) - Math.log(denom)) /* e^{ln(num) - ln(denom)} */

Suppose,
x = num/denom
Taking natural log,
ln(x) = ln(num/denom) = ln(num) - ln(denom)
e^{ln(x)} = e^{ln(num) - ln(denom)}
Answer,
x = e^{ln(num) - ln(denom)}

- Anant November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mathematically seems correct. but it's not giving correct result somehow.
Math.log(625) gives Infinity.

- ashpiy November 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int division(int x, int y);

int main()
{
	int numerator = 0;
	int denominator = 0;
	int res = 0;
	printf("Enter numerator and denominator: \n");
	scanf("%d%d",&numerator,&denominator);

	if(denominator == 0)
	{
		printf("Denominator can't be zero in a division..\n");
		return 1;
	}
	res = division(numerator, denominator);
	printf("Integer Division Result= [%d]\n",res);
	return 0;
}

int division(int numerator,int denominator)
{
	int count = 0;
	if(numerator < denominator)
	{
		return count;
	}
	while(numerator > denominator)
	{
		numerator = numerator - denominator;
		count++;
	}
}

- Jit November 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Func division(...) may also include a test if denominator = 2, than whole operation will be just a shift: numerator >>= denominator;

- Anonymous November 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream.h>
#include <math.h>

using namespace std;

float divide(float,float);

int main()
{
while(1)
{
float num, denom;
cout<< "Enter numerator: ";
cin >> num;
cout<< "Enter denominator: ";
cin >> denom;
float x;
cout<< divide(num,denom)<< endl;
}
}

float divide(float num, float denom)
{
return (num*pow(denom,-1));
}

- kyrax November 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

comment out the float x;. something i forgot to delete

- kyrax November 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int divide(int num)
{
int result = num >> 1;
return result;
}

This can be used for dividing a number by 2.

- Radhika November 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use binary search to solve this problem. The already existing solutions presented here run in O(numerator/deno). If denominator is 1 then the worst case time complexity will be O(numerator) which is very bad. The solution using the binary search can solve the problem in O(log(numerator)).
The problem can be restated as follows. Find the largest number x such that q*x<=p.Here I assume that p,q are positive numbers. The following is the code.
int p,q;
int division(int start=0,int end = p){
if(start>end)
return -1;
int mid = (start+end)>>1;
if(q*mid<=p)
return max(mid,division(mid+1,end));
else
return division(start,mid-1);
}

- madhav November 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't give incomplete solution. What is the value of q? You are never initializing it so it will always be 0.

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

"codegambler.wordpress.com/2009/08/11/division-operation-without-using-division-operator/"

- pankaj November 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int divideWithoutSymbol(int numerator, int denominator){
		int counter = 0;
		int addedUp = 0;
		
		while(addedUp <= denominator-numerator){
		addedUp += numerator;
		counter++;
		}

		return counter;
	}

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

A solution using bit manipulation:

The following code works for positive numbers. When the dividend or the divisor or both are negative, have flags to change the sign of the answer appropriately.

    int divi(long long m, long long n)
    {
        if(m==0 || n==0 || m<n)
            return 0;
        long long  a,b;
        int f=0;
        a=n;b=1;
        
        while(a<=m)
        {
            b = b<<1;
            a = a<<1;
            f=1;
        }
    
        if(f)
        {
            b = b>>1;
            a = a>>1;
        }
    
        b = b + divi(m-a,n);
        return b;
    }

- tinybells January 25, 2014 | 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