Interview Question


Country: United States




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

import org.junit.Test;

import java.util.stream.IntStream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class Fact001 {
    public int fac(int n) {
        return n == 1 ? 1 : n * fac(n-1);
    }

    public int[] all(int n) {
        return IntStream.rangeClosed(1, n).map(i -> fac(i)).toArray();
    }

    @Test
    public void test() {
        assertThat(all(3), is(new int[]{1,2,6}));
    }


}

- yny.all March 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fac(int n) {
int ans;
if(n==1) {
printf ("1 : %d",n);
return 1;
}
ans = n * fac(n-1);
printf ("%d : %d",n,ans);
return ans;
}

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

int fact(int x){
if(x==0)
return 1;
else
return x * fact(x-1);
}

- rosejk March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{

import java.util.Scanner;

public class FactorialNum {

	public static void main(String[] args) {

		long temp, temp1;
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the integer yo want to have factorial: ");
		temp = input.nextLong();
		temp1 = thatFact(temp);
		System.out.println(temp1);

	}

	public static long thatFact(long test) {

		long temp = 1, i, k;
		for (i = 0; i <= test; i++) {
			temp = test * (test - 1);
			for (k = (test - 1); k > 1; k--) {
				temp = temp * (k - 1);
			}
		}
		return temp;
	}

}}

- Shashank March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{{{{ import java.util.Scanner; public class FactorialNum { public static void main(String[] args) { long temp, temp1; Scanner input = new Scanner(System.in); System.out.println("Enter the integer yo want to have factorial: "); temp = input.nextLong(); temp1 = thatFact(temp); System.out.println("The Factorial of the given number " + temp + " is : "+temp1); } public static long thatFact(long test) { long temp = 1, i, k; for (i = 0; i <= test; i++) { temp = test * (test - 1); for (k = (test - 1); k > 1; k--) { temp = temp * (k - 1); } } return temp; } } }}}}}}}} - shashank March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- ms March 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Solution(object):
    def fac(self,n):
        if n == 1 or n == 0:
            return 1
        return n*self.fac(n-1)

- ziqiyang88 March 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int factorial(int n){

if(n==0){
printf("\nfactorial of 0 = 1");
return 1;
}

else{
int fact = n * factorial(n-1);
printf("\nfactorial of %d = %d",n,fact);
return fact;

}

}

- a.mookambika June 10, 2016 | 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