Cognzant Technology Solutions Interview Question for Android Engineers


Country: India
Interview Type: Written Test




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

public static long calculate(int n){
    int sum = 0;
    for(int i=1, factorial=1, sign=1; i <= n; i++, factorial*=i, sign*=-1)
  	  sum += sign*factorial;
    return sum;
  }

- Roman August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Observation:
As there are only 3 elements of series it's hard to assume anything. But if the assumption of deducting even factorial from the previous odd factorial.
here is the solution with O(1) space and O(n) time

def fancyFactorial(n):
    
    n1fact = 1
    result = 1
    if n <= 1:
        return n1fact
    
    i = 2
    while i <= n:
        n1fact = n1fact * i
        if i % 2 == 0:
            result = result - n1fact
        else:
            result = result + n1fact
        i += 1
    
    return result

- nil12285 August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Swift

///func factorial(n:Int, result:Int = 1)->Int {
///    if n < 1 { return result }
///    return factorial(n - 1, result: result * n)
///}

///func nfactorial(n:Int, result:Int = 0)->Int {
///    if n < 1 { return result }
///    return nfactorial(n - 1, result: factorial(n) + result)
///}

nfactorial(5) // 153



// I did read the problem wrong. Need new glasses. Thanks!

func factorial(n:Int, result:Int = 1)->Int {
    if n < 1 { return result }
    return factorial(n - 1, result: result * n)
}

func nfactorial(n:Int, val:Int = 1, result:Int = 0)->Int {
    if val > n { return result }
    let f = factorial(val)
    let m = val % 2 == 0 ? (result - f) : (result + f)
    return nfactorial(n, val:val + 1, result:m)
}

- hatebyte August 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You read the problem incorrectly.

- helloru August 16, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

'use strict';

function compute(n) {
    if (n <= 1) return 1;
    let factorial = 1;
    let result = 0;

    for (let i = 1; i <= n; i++) {
        factorial = factorial * i;
        if (i % 2 == 0) {
            result = result - factorial;
        }
        else {
            result = result + factorial;
        }
    }

    return result;
}

- piechur August 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Swift 2.2. Doesn't account for overflow.

func sumOfFactorialsTo(n: Int)-> Int {
    var result = 0
    var factorial = 1
    for i in 1 ... n {
        factorial *= i
        result += (i % 2 == 0 ? -1 : 1) * factorial
        
    }
    return result
    
}

- helloru August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// return 1! - 2! + 3! - 4!... n!
	public static int calculate(int n) { 
	  int []factorial = new int[n];
	  factorial[0] = 1; //1!

	  int result = factorial[0];
	  for(int i=2;i<=n;i++) { // i = 1
	      factorial [i-1] = factorial[i - 2] * i;
	      if(n % 2 == 0) {
	        result -= factorial[i-1];
	      } else {
	        result += factorial[i-1];
	      }
	  }
	  return result;
	}

Time Complexity : O(n) for iterations, O(1) - constant access for array
Space Complexity : Additional space of size n

- coder145 August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getFactorialSum(int n){
		
		if(n == 1) return 1;
		
		int factorial = 1;
		int cummlativeSum = 0;
		for(int j = 1; j <=n; j++){
			factorial = factorial * j;
			if(j % 2 != 0) cummlativeSum = cummlativeSum - factorial;
			cummlativeSum = cummlativeSum + factorial;
		}
		return cummlativeSum;
	}

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

Calculates the new Factorial in every iteration

public int Calculate(int n)
{
	int total = 0;
	int fact = -1;
	
	for (int i=1; i <=n; i++)
	{
		fact *= -i;
		total += fact;
	}
	
	return total;
}

- hnatsu August 22, 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