Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Sergey is right. Just adding another interesting mix :

/* 
With extra space, again exact in 2n
Given array not sorted 
*/
def single_pass( arr , S ){
   s = set( arr ) // create a set out of it 
   // find item x in arr such that S -x is in the set!
   v = find ( arr ) :: { (S - $.item) @  s }
   v.nil ? 'Nothing' : v.value 
}

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

A fairly simple question. I proposed 3 approaches:

1) Brute Force: For each element in the array search all the remaining elements for (Sum - the chosen element). O(N^2) is the Time Complexity.

2) (a) Sort the array and use 2 references one from the start and the other from the end.
(b) If sum of both the referenced element is less than given sum then increment the position of the smaller reference.
(c) If sum of both the referenced element is greater than given sum then decrement the position of the larger reference.
(d) Repeat (b) & (c) until both references cross each other.
O(N*Log N) - For Sorting + O(N) searching with references = Total (N*Log N)

3) Use a HashTable (assume a HashMap). In the first pass, put each element into the HashTable. In the second pass, for each element, search for the element (Sum - current element). At the end of the two passes we will have our solution.
Time Complexity is O(N)

I was asked to assume memory is a constraint, so ended up writing code for the proposal (2) using Sorting.

- teli.vaibhav October 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If array is sorted this could be solved in O(n) complexity.

Python solution:

def pair_of_sum(A, S):
    left = 0
    right = length(A) - 1
    while left < right:
        current_sum = A[left] + A[right]
        if current_sum == S:
            return left, right
        if current_sum < S:
            left += 1
        if current_sum > S:
            right -= 1

If array is not sorted we need to sort it and total complexity will be O(n log n)

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

there are three ways of aproach

int[] input={2,7,5,3,0,8,1};
		int traget=9;
		//if no extra  memmory is allowed
		
		for(int i=0;i<input.length;i++)
		{
			for(int j=i+1;j<input.length;j++)
			{
				if(traget==input[i]+input[j])
				{
					 System.out.println("sol 1  Pair with given sum " +
							  traget + " is (" + input[i] +
		                      ", "+input[j]+")");
				}
			}
				
				
		}
			
		
		
		
		
		
		
		
		
		
		
		//if no idea about maximum element
		
		Hashtable<Integer,Integer>indexvaluemap=new Hashtable<Integer, Integer>();
		
		for (int i = 0; i < input.length; i++) {
			indexvaluemap.put(input[i],i);
			
		}
	
	  for(int i=0;i<input.length;i++)
	  {
		  if(indexvaluemap.get(traget-input[i])!=null&&indexvaluemap.get(traget-input[i])!=i)
		  {
			  System.out.println("sol2 Pair with given sum " +
					  traget + " is (" + input[i] +
                      ", "+(traget-input[i])+")");
		  }
	  }
		
		
	
		//if maximum  value of elements in an array is known
	    int MAX = 100000;
		
		boolean[] binmap = new boolean[MAX];
 
        for (int i=0; i<input.length; ++i)
        {
            int temp = traget-input[i];
 
            // checking for condition
            if (temp>=0 && binmap[temp])
            {
                System.out.println("sol3 Pair with given sum " +
                		traget + " is (" + input[i] +
                                    ", "+temp+")");
            }
            binmap[input[i]] = true;
        }

- sj October 30, 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