Amazon Interview Question for Software Engineer / Developers






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

unsigned long CountZerosOfFactorial(unsigned long n)
{
unsigned long x2 = n, n2 = 0;

while (x2)
n2 += x2 /= 2;

unsigned long x5 = n, n5 = 0;

while (x5)
n5 += x5 /= 5;

return min(n2, n5);
}

- fiddler.g June 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@fiddler.g

you don't need to check condition for 2. This should suffice.

while (x5)
n5 += x5 /= 5

return n5;

- Patron June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

r = 0
for i = 1, n
   k = i
   while k % 5 =0
      r = r + 1
      k = k/5

return r

=> O(.)=sum(log(i)) = O(n log n)

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

I don't think we have to use any while loop, simply divide the number by 5 and quotient is the number of zero's at end.
So,
int numZero( int n){

return n/5;

}

- Patron June 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please ignore above one.

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

What is wrong with your solution? I believe it is correct.

- Why ignore June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Trivially broken for 25! = 15511210043330985984000000, which has 6 zeros on the end.

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

sum=n/power(5,0)+n/power(5,1)+n/power(5,2) and so on .untill ou get 0 so sum will be no of zero

- ashu143 June 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

suppose if n=25,
n/power(5,0) will return 25.And this is wrong. The solution is sum=n/power(5,1)+n/power(5,2) and so on .untill ou get 0 so sum will be no of zero

- chandrasekhar July 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is the same problem as question id=2577

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

{{
The idea is to find the number of 5's in the n!.

size_t getTrailingZeroes(size_t n)
{
size_t count = 0;

for (size_t div = 5;; div *= 5)
{
size_t result = n/div;

if (result == 0)
break;

count += result;
}

return count;
}
}}

- sisharma80 July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea is to find the number of 5's in the n!.

size_t getTrailingZeroes(size_t n)
{
	size_t count = 0;
	
	for (size_t div = 5;; div *= 5)
	{
		size_t result = n/div;

		if (result == 0)
			break;

		count += result;
	}

	return count;
}

- sisharma80 July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fun(n):
       if n == 1:
          return 1
       if n == 2:
          return 2
       return fun(n-1) * n
    def main():
        n = 10
        resultList = list(fun(10))
        zeroCounter = 0
        for element in resultList:
            if element == "0"
               zeroCounter += 1
        print "answer:",zeroCounter

- weijiang2009 February 06, 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