A9 Interview Question for Software Engineer / Developers






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

1. Multiply all numbers.
2. Divide the product by each number in array.

Eg.

arr[] = {2, 3, 4, 5};
prod_all = 2 * 3 * 4 * 5;
prod_1 = prod_all / 2;
prod_2 = prod_all / 3;
prod_3 = prod_all / 4;
prod_4 = prod_all / 5;

- Vaibhav April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

prod[i] = (a[0]*a[1]*...*a[i-1]) * (a[i+1]*...*a[n-1]) = prefix[i] * postfix[i]
time complexity: O(n);

int *ProdArray(int *a, int n)
{
	int *prefix, *postfix;
	int i;

	prefix = (int*)malloc(sizeof(int)*n);
	postfix = (int*)malloc(sizeof(int)*n);
	prefix[0] = 1, postfix[n-1] = 1;
	for(i=1; i<n; i++){
		prefix[i] = prefix[i-1]*a[i-1];
		postfix[n-i-1] = postfix[n-i]*a[n-i];
	}
	for(i=0; i<n; i++)
		prefix[i] = prefix[i]*postfix[i];
	free(postfix);
	return prefix;
}

- steven.shi April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

plz explain the question with eg. R u saying product of all element except ith element?

- Anonymous April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

steven.shi's methods should work, but Vaibhav's would fail if there's 0's in the array.

- ftfish April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You have to check for 0 in the array and take care of that as well!

- A M April 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know it is not mentioned in the question, but is it solvable in O(1) space? What if that comes up as a follow up question?

- Abhishek April 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

flag=0;
prod=1;
for(i=0;i<n;i++)
{
if(arr[i]==0)
{flag=1; break; }
prod*=arr[i];
}
if(flag)
{
arr[i]=prod;
for(j=0;j<i;j++)
arr[i]=0;
for(j=i+1;j<n;j++)
{arr[i]*=arr[j];
arr[j]=0;}
}
else
{
for(i=0;i<n;i++)
arr[i]=prod/arr[i];
}

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

<pre lang="" line="1" title="CodeMonkey25723" class="run-this">/* I think this should cover the edge cases */

def prod( myList ):
prod = 1
numZeros = 0
for a in myList:
if a != 0:
prod *= a
else:
numZeros += 1
return prod, numZeros

def prodN1( myList ):
if not len( myList ):
print 0
total, zeros = prod( myList )
for i,a in enumerate( myList ):
if a == 0:
if zeros == 1:
print 'Product without element', i, total
else:
print 'Product without element', i, 0
else:
if zeros == 0:
print 'Product without element', i, total/a
else:
print 'Product without element', i, 0 </pre><pre title="CodeMonkey25723" input="yes">
</pre>

- Anonymous December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class App {
	public static void main(String[] args) {
		int[] arr = {1, 2, 3, 4, 0};
		double product = 1;
		double counter = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] != 0) {
				product = product * arr[i];
				counter++;
			}
		}
		System.out.println("Answer: " + Math.pow(product, --counter));
	}
}

- cshreyas April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int k=0;k<=n;k++)
{for(int i=0;i<n;i++)
t=c[k];
c[k]=1;
product=product*c[i];

}
c[k]=t;
}

- Anonymous May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public double getProductOfGroups(int[] inputArray)
	{
		if(inputArray == null)
			return Integer.MIN_VALUE;
		
		int n= inputArray.length;
		
		if(n==0)
			return Integer.MIN_VALUE; 
		
		double res = 1;
		
		for(int eachNumber : inputArray)
			res*= eachNumber; 
		
		return Math.pow(res, n-1);
		
	}

- teli.vaibhav June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Time: O(n),	space: O(1)
	public int [] getProductOfGroups(int[] Array)
		{
			if(Array == null||Array.length<=1)
				return null;
			int [] ret=new int [Array.length];
            boolean isZero=false;
            int zeroIndex=-1;
            int total=1;
            for(int i=0;i<Array.length;++i)
				if(Array[i]==0){
					isZero=true;
					zeroIndex=i;	
				}
	        for(int i=0;i<Array.length;++i){
				if(i!=zeroIndex)
					total*=Array[i];	
			}
	                if(isZero){
				ret[zeroIndex]=total;
			}
			else{
				for(int i=0;i<Array.length;++i)
					ret[i]=total/Array[i];
			}

			return ret;                
			
		}

- jyin@cs.unc.edu January 29, 2015 | 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