Amazon Interview Question for Software Engineer / Developers






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

long factorial(int n){
value = 1;
while(n > 0){
value = value * n;
n = n - 1;
}
return result;
}

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

sorry,

it should return value not result

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

long factorial(int n) {
long[] values = new long[n+1];
values[0] = 1;
for (int i = 1; i <= n; i++) {
values[i] = i * values[i - 1];
}
return values[n];
}

- yangyang4j@gmail.com March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

class FactorialHolder {
  private static final int SIZE_OF_ARRAY = ??; // maximum of n satisfying n! < MAX_LONG
  private static long[] values = new long[SIZE_OF_ARRAY];
  static {
    for(int i=0;i<SIZE_OF_ARRAY;i++) {
       values[i] = 0;
    }
    values[1] = 1;
  }
  public static long factorial(int n) {
    return values[n]>0 ? values[n] : values[n]=factorial(n-1)*n;
  }
  public static long factorialIterative(int n) {
    int i;
    for(i=n;i>=1;i--) {
      if (values[i]!=0) break;
    }
    for(i=i+1;i<=n;i++) {
      values[i] = values[i-1]*i;
    }
    return values[n];
  }
}

- Zaphod March 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you can can use c++, template meta programming could be better, because factorial table is built at compile time(not runtime).

even in java, factorial value is fixed, you can build table and paste into source code.(and earn calculation time)

- Zaphod March 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Problem:
-----------
Find Nth factorial

Example:
------------

N=5

Factorial F = 5*4*3*2*1 = 120

Algo/Solution:
-----------

Input : Factorial int N

A for loop which will decrement i from N to 0
and keep on multiplying with its value

Output : result


Code:
--------------

Private int NFactorial(Int N)
{

For (int i=N , i > 0,i-- )
{
int result *=i;
}

return Result;

}


HandRun:
--------------
N=5
i=5
5
5*4=20
20*3=60
60*2=120
120*1=120

Order:
-----------
O(N)

- tarun phaugat March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fact(int n)
{
return (n<=1?1:fact(n-1)*n);

}

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

What will you do if there is an over flow ?

- abhimanipal March 09, 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