Google Interview Question for Software Engineer / Developers






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

Given: Two arrays A[1...n] and B[1...m]

Case 1: Two arrays sorted Solution is O(m+n) - Linear

Case 2: A is sorted and B is not sorted 
   Sol 1: No Hashing - O(mlogn) - No Extra Space
   Sol 2: Hashing - O(m+n) - Using Extra Space

Case 3: Both A & B are not sorted
   Sol 1: Hash Table - O(m+n) - Extra Space
   SOl 2: Sort A - O(nlogn + mlogn)

- Nachiketha May 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that makes sense

- Anonymous August 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how could it be O(n)...even if the arrays are sorted .The worst case time complexity is going to O(n2)

- Anonymous May 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To get O(N) with a sorted array you could go through the arrays side by side.

If one of the array's current element is greater than the other array's current element, increment the array with the smaller value until it's current value reaches or exceeds that of the first array. Each time you increment, you can determine if the current element is in both arrays. Do this until reach the end of the smaller array.

The complexity would have been O(N) - N being the size of the smaller array.

Worst case would come if you had an unsorted array - you would sort (O(logN)) then do the above (O(N)) bringing the worst case to O(NlogN).

- anon May 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, O(NlogN) ends up being O(N).

- anon May 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Loop through each item in Array A and do a binary search on Array B
So maximum time taken is O(m * log n)
m is size of A and n is size of B. no space is required, not even position of elements is changed in this case.

- K2G May 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

K2G, your solution wouldn't work for unsorted arrays.
Another approach: (independent of the order of Arrays - works for both sorted/unsorted arrays)
create a Binary Search Tree from Array A.
Now, search each element from array B. ( This is the best way to find duplicates)
BST creation - O(n)
searching m elements in a tree with n elements ( mlg n)
overall time complexity O(n + m lg n) -> m lg n

- Anonymous May 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, that was for sorted array only

BST Creation takes much time i suppose.
O(logn) for each node insertion.
so for inserting n nodes it should take O(nlogn)

- K2G May 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorting any array takes nLog(n)..

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

There are faster ways for...
From:)
http://forums.devarticles.com/c-c-help-52/how-to-compute-set-intersection-efficiently-59122.html
-----------
If duplicates are counted as one i.e. {1 2 3 3} = {1 2 3}:

1.) Assign a bit value for each item.
2.) Form two bit strings.
3.) Perform a bitwise & operation.
4.) Convert back into original.

e.g. {tom **** harry} n {harry harry fred}
Key: tom => 1 X 10^0
**** => 1 X 10^1
harry => 1 X 10^2
fred => 1 X 10^3

==> 0111 & 1100 => 0100
=> {harry}

Give credit or discredit to the original poster.

- Lord Darth Plaguies June 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But how to you assign a big value to each item? Doesn't this need to use hash table, or some kind of sorting, BST, etc?

- anonymous September 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code:
Assumption: Given 2 sorted arrays a, b of length len1 and len2 respectively

int m=0, n=0;
   while(m < len1 && n < len2)
   {
     if(a[m] == b[n])
     {
       printf("%d ", a[m]);
       m++;
       n++;
     }
     else if(a[m] > b[n])
     {
        n++;
     }
     else 
     {
        m++;
     }     
   }

Iteration is over the minimum of lengths of 2 arrays, so complexity is O(min(m,n))

- MakeItWork August 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that only works if array has no repeated elements.

- bigz September 04, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Iteration is over the minimum of lengths of 2 arrays, so complexity is O(min(m,n))"

The above statement is not correct. Let's say, length of array A is 1 whose value is a big number like 1 billion and the length of array B is 1 million whose last element's value is 10 million. In this case you have to iterate over all the elements in array B!!!

- srikon September 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i, j = 0, 0
a = [-9, -8, 0, 1, 4, 6]
b = [-8, -7, -6, 0, 1, 3, 5, 6]

while (i < a.size && j < b.size) do
    if a[i] == b[j]
        #puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing i and j"
        puts a[i]
        i += 1
        j += 1
    elsif a[i] < b[j]
        #puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing j"
        i += 1
    elsif b[j] < a[i]
        #puts "a[i] = #{a[i]} and b[j] = #{b[j]} Incrementing i"
        j += 1
     end
end

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

Put all elements of first into hashtable. For each ele in
Second array chk if it is present in table . If yes put into
Result arrray else continue.
Correct me...

- miandfhyu July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the best algorithms for finding the intersection of two arrays when the arrays are sorted is applying binary search to find all the elements of the other array in the first array and print the elements which are common in both the arrays:

Imlemenatation:

bool binary-search(int arr[], int low, int high, int key){
	if(high >= low){
		int mid = low + (high - low) / 2;
		if(arr[mid] == key)
			return true;
		if(key > arr[mid])
			return binary-search(arr, mid + 1, high, key);
		else
			return binary-search(arr, low, mid - 1, key);
	}
	return false;
}
int main{
	for(int i = 0; i < m; i++){
		if(binary-search(arr_1, 0, n, arr_2[i]) == true)
			cout<<arr_2[i]<<" ";
	}

Time complexity will be O(logn).
Space complexity will be O(1).

And while the array is not sorted then we can use a hashset to find the common elements.

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

sorted:
O(N)
unsorted:
first sort O(log(n))
Then O(N)
So totally O(n)

is that correct?

- futureme May 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain how it works in your case..

- Srikanth May 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude how you sort in O(log(n)).
kindly share your research paper

- abcd March 09, 2013 | Flag


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