Yahoo Interview Question for Interns


Country: Cananda
Interview Type: In-Person




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

nums = [2,3,1,4,3,2]
res = 1
for num in nums:
    res *= num
for num in nums:
    print(res / num)

- Anonymous October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

nums = [2,3,1,4,3,2]
res = 1
for num in nums:
    res *= 1
for num in nums:
    print(res/num)

- Master_Odin October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

Do remember that this solution will break if there are zeros in an array. These are special cases which interviewer looks for us to handle.

Here is sample code (similar solution) which handles zero possibility.

def mul(array):
 	array_mul = 1
 	has_zero = False
 	for i in array:
 		if i == 0:
 			has_zero = True
 		else:
 			array_mul *= i
 	for i in array:
 		if has_zero is True and i != 0:
 			print 0
 		elif has_zero is True and i == 0:
 			print array_mul
 		else:
 			print array_mul/i

- Saurabh October 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int[] tab = {2,3,1,4,3,2};
int p = 1;
for (int i=0;i<tab.length;i++)
   p = p * tab[i];

for(int i =0;i<tab.length;i++)
    console.writeline(p/tab[i]);

- Anonymous October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you can do this is 0(n)..

1. run through the loop to compute product which is the product of all the numbers.
2. Once you have product, run through a loop and divide product/arr[i] and and add it to a new array with the results.

- PS October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// ZoomBA
nums = [  2,3,1,0, 4,3,2 ]
def do_yahoo( nums ){
  #(num_zeros, product)  = lfold ( nums ,[0, 1] ) -> {  
    if ( $.o == 0 ){ $.p.0 += 1 } else { $.p.1 *= $.o } 
    $.p  
  }
  product = num_zeros > 1 ? 0 : product 
  for ( nums ) {
    println ( $ == 0 ? product : (product/$) )
  }
}
do_yahoo ( nums )

Note the handling of zeros. Obviously, if there is one zero, the problem is non trivial. For multiple zeros, the problem is trivial again.

- NoOne October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] tab = {2,3,1,4,3,2};
		for (int i = 0; i < tab.length; i++) {
			int res = 1;
			for (int j = 0; j < tab.length; j++) {
				if (i != j) {
					res = res * tab[j];
				}
			}
			System.out.println(res);
		}

- Omar October 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] tab = {2,3,1,4,3,2};
		for (int i = 0; i < tab.length; i++) {
			int res = 1;
			for (int j = 0; j < tab.length; j++) {
				if (i != j) {
					res = res * tab[j];
				}
			}
			System.out.println(res);
		}

- Omar October 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] tab = {2,3,1,4,3,2};
		for (int i = 0; i < tab.length; i++) {
			int res = 1;
			for (int j = 0; j < tab.length; j++) {
				if (i != j) {
					res = res * tab[j];
				}
			}
			System.out.println(res);
		}

- Omar92 October 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] tab = {2,3,1,4,3,2};
	int product = 1;
	
	for(int i=0; i < tab.length; i++){
			product  = product *  tab[i];	
	}

	for(int i=0; i < tab.length; i++){
		System.out.print(product/tab[i]+" ");
	}

- AkshayB October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] productExceptSelf(int[] nums) {
        if(nums.length == 0){
            return new int[0];
        }
        int[] result = new int[nums.length];
        result[0]=1;
        for(int i=1;i<nums.length;i++){
            result[i]=result[i-1]*nums[i-1];
        }
        int product = 1;
        for(int i=nums.length-1;i>=0;i--){
            result[i]=result[i]*product;
            product = product*nums[i];
        }
        return result;
    }

- munir1690 October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To solve this, we have to use two loops. Is it possible to do it with one loop? Any leads?

- Sp October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is possible with one loop

- Sp October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it possible to do it with one loop?

- Sp October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[]={2,3,1,4,3,2},pro;
for(int i=0;i<6;i++){
pro=1;
for(int j=0;j<6;j++)
if(i!=j) pro*=a[j];
cout<<pro<<endl;
}

- ARUN KG October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printProduct(int[] arr) {
	int sum = 0, j = 0;
	for (int i = 0; i < arr.length; i++) {
		while (j < arr.length) {
			if (j == i) continue;
			sum *= arr[j];
			j++;
		}
		System.out.println(j);
	}
}

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

import java.util.Arrays;

public class ProductWholeArrayExceptElement {
	
	public static void main(String[] args){
	int[] arr = {2,3,4,1,5,2};
	System.out.println(Arrays.toString(printProductExceptElement(arr)));
	}

	static int prod = 1;
	
	private static int[] printProductExceptElement(int[] arr){
		int[] result = new int	[arr.length];
		
		for(int i=0;i<arr.length;i++){
			prod *= arr[i];
		}
		for(int i=0;i<arr.length;i++){
			result[i] = prod/arr[i];	
		}
		return result;
	}
}

Complexity - O(n)

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

The same as all previus posts

public static int[] GetProducts(int[] a)
{
	if (a == null || a.Length == 0)
		return new int[0];
	
	long product = 1;
	foreach (var numb in a)
		product *= numb;
	
	int[] result = new int[a.Length];
	for (int i=0; i < a.Length; i++)
		result[i] = (int)(product / a[i]);
	
	return result;
}

- hnatsu October 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem we solve o(n) time complexity and o(1) space complexity but if anyone has better solution so please share thier solution.

printNum()
	{
		int arr[]={2,3,1,4,3,2};
		int size=sizeof(arr)/sizeof(arr[0]);
		for(i=0;i<size;i++)
		{
			mul = mul*arr[i];
		}
		for(i=0;i<size;i++)
		{
			printf("%d\n",mul/arr[i]);
		}
	}

- rsingh October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		int [] arrays = {2,3,1,4,3,2};
		int sum = 1;
		for( int i = 0; i < arrays.length; i++ ){
			sum = sum * arrays[i];
		}
		
		for( int i = 0; i < arrays.length; i++ ){
			System.out.println( sum / arrays[i]);
		}
	}

- puttappa.raghavendra December 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my $length=@ARGV;
@ARRAY=@ARGV;
for(my $i = 0; $i <= $length-1; $i++)
{
        splice(@ARGV,$i,1);
        print eval(join ("*",@ARGV));
        print "\n";
        @ARGV=@ARRAY;
}

- usha March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
int [] nums = {2,3,1,4,3,2} ;
int product = 1;
for (int x : nums) {
product*= x;
}
for (int x : nums) {
System.out.println(product/x);
}
}

- Anonymous May 31, 2017 | 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