Google Interview Question for Software Engineer / Developers






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

Say you have two lists with n and m elements.

First get a= List1[ceil(n/2)], b= List2[ceil(m/2)]
If a>=b, median must be lesser than a and greater than b..
therefore now consider elements of List1 lesser than a, elements of List2 greater than b
If a<b median must be greater than a and less than b..
therefore now consider elements of List1 greater than a, elements of List2 lesser than b

Continuing this say, in each iteration, you eliminate half of each list.
O(logn) + O(logm) ... if n > m, O(log n) else O(log m)

- Coder November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and your stopping condition is...

- Anonymous December 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is wrong and works only for the arrays with the same size.
Counterexample may be 1,2,3.....20 vs 10000,10001,10002,10003,10004,10005,10006. Median in the first is 10,5 and in both should be 14,5. However already after the second median check we will be looking for the median in 15-20 vs 10000 - 100001. This is because the jumps in each array are not the same and therefore you may end up skipping more elements in the first array that are contained in the second array.

- mbriskar May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is this solution as per the MIT problem set

1st result of Google Search for "median of 2 sorted arrays"

ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/30C68118-E436-4FE3-8C79-6BAFBB07D935/0/ps9sol.pdf

- bitmapper December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note that they consider only arrays of the same size

- mbriskar May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Much better explanation here - geeksforgeeks.org/?p=2105

- bitmapper December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

note they consider only arrays of the same size and that makes a difference

- mbriskar May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use the merge procedure of the mergesort. We don't need to do the actual merge but just keeping track of the number of elements considered for merge will solve the problem. This is an O(n) time solution.
O(lgn) might be possible through binary search. Thinking about it...

- Thiyaneshwaran S November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find median = (len(arr1)+ len(arr2))/2
Place two pointers at the bottom of both the array (arr1_p,arr2_p) .
initialize i=0
pick the move the pointers by this algo
1. compare arr1[arr1_p] with arr2[arr2_p] ...
2. do -1 to the pointer with lesser value and i++
3. if i < median goto 1 else choose the pointer with higher value
OR
in case of even size do (arr1[arr1_p] + arr2[arr2_p])/2

0(n/2)

- pk November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Most of the algorithms are using the median checks that work only for the arrays of the same length.

For the arrays of different length you must use different approach, the one that is described http://www.drdobbs.com/parallel/finding-the-median-of-two-sorted-arrays/240169222?pgno=2 or in video here https://www.youtube.com/watch?v=_H50Ir-Tves (ignore the (3), there is a mistake because the guy also consider comparing medians to work for different arrays, even though it does not work).

However I think you cannot easily continue recursively in the subarrays as he says in the video, it's safer to just make the range a,b and a2,b2 always smaller instead (but not to forget A.length and B.length values). I don't have a proof though

- mbriskar May 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For finding the median of two sorted arrays we will follow the algorithm given below:

Implementation:

int return_median(int a[], int n, int b[], int m){
	int median, min_index = 0, max_index = n;
	while(min_index <= max_index){
		int i = (min_index + max_index) / 2;
		int j = ((n + m + 1) / 2) - i;
		if(j > 0 && i < n && b[j - 1] > a[i])
			min_index = i + 1;
		else if(i > 0 && j < m && b[j] < a[i - 1])
			max_index = i - 1;
		else{
			if(i == 0)
				median = b[j - 1];
			else if(j == 0)
				median = a[i - 1];
			else
				median = max(a[i - 1], b[j - 1]);
		}
	}
	if((n + m) % 2 == 1)
		return (double)median;
	if(i == n)
		return (median + b[j]) / 2.0;
	if(j == m)
		return (median + a[i]) / 2.0;
	else
		return (median + min(a[i], b[j])) / 2.0;
}

- swapnilkant11 July 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I mentioned the above methods..But I found O(logn) soultion for the same..:-(

- Partha November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I mentioned the above methods..But I found O(logn) soultion for the same..:-(

- Partha November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I mentioned the above methods..But I found O(logn) soultion for the same..:-( on the internet after my interview..

- Partha November 10, 2009 | 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