Google Interview Question for Software Engineer / Developers






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

Can be done without recursion.

Something like...

i = 0;
j = 0;
  
while (i < la && j < lb) {
    if (a[i] == b[j]) {
        print(a[i]);
        i++;
        j++;
    }
     
    if (a[i] > b[j]) 
        j++;
    else 
        i++;
}

- Anonymous January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Just note that the second 'if' should be 'else if'.

- Anonymous January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void insertionOfTwoArrays(int data1[], int data2[]){
		int i = 0, j = 0;
		int prev = data1[0] < data2[0] ? data1[0] - 1 : data2[0] - 1;
		while (i < data1.length && j < data2.length) {	
			if (data1[i] == data2[j] && prev != data1[i]){
				System.out.print(data1[i]+ " ");
				prev = data1[i];
				i++;
				j++;
			}
			else if(data1[i] < data2[j]){
				i++;
			}else {
				j++;	
			}
		}
	}

- Adnan Ahmad September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion..

- Randon January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

m log n where m<n sizes

- Anonymous January 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No! O(M+N)

- Anononomy January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys, I think this is how it should be., though i have not tested yet.
void intersect(int *a, int *b, int la, int lb)
{
int l;
int size;

if(la>lb)
{
l = la - lb;
size = la;
}
else
{
l = lb - la;
size = lb;
}

for(int i = l ; i< size; i++)
{
for(int j = 0; i< l; j++)
{
if(a[i]! = b[j])
return;
else
{
cout<<"Intersection element is"<<a[i];
}
}
}
}
Suggestions most welcome....

- crazy_bee January 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we can recursion method so we can do that

- john February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why recursion? o(m+n) solution appears fine.

- Sumeet February 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void intersectOfSortedArrays(a,b) {
int i = 0;
int j = 0;

while (i < a.length && j < b.length) {
if (a[i] == b[j]) {
System.out.print(a[i] + " : ");
i++;
j++;
continue;
}
if (a[i] < b[j])
i++;
else
j++;
}

- ashi_420 February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since both the arrays are sorted, how about binary searching elements of smaller array(say 'a') in the bigger array (say 'b'), also skipping search for each element of 'a' which is smaller than the first element of 'b' or greater than last element of 'b'. thus giving a complexity of O(lalog(lb)) what say

- Anonymous February 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sounds better in terms of time complexity.

- volongoto February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is O(mlogn).. a better solution O(m+n) is already stated above.

- neo April 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not O(mlogn) it is O(m + logn)

logn1 + logn2 + ... lognn = logn

- dantepy May 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use hash table. Insert integers from a[] into this hash table. insert items from b[] again, see if a collision occurs. That is the common element. Put than in c[] which is a intersection of a[] and b[]

- Anonymous February 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity would be O(m+n)

- Anonymous February 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo by Ashi works.
If we wanna do by map, i guess Anonymous's method can be improved by...
hash contents of array1 with count of a particular element as the value.
iterate over array2. If element is found- check if count > 0, decrement count and write to o/p.
Example: array1: 1 2 2 5 6 6 6 , array2: 1 1 2 6 6 7 , then o/p: 1 2 6 6.
-Hash contents of first array:
val count
1 2
2 1
6 2
7 1

iterate over array2. - found 1 so check if count > 0 , decrement count , write to o/p.
Not sure if binary search will work in this case. It wud produce 1,2,6 as the result not 1, 2 , 6, 6 .

- game. March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using hash table you don't need the arrays to be sorted, so this doesn't seem good.

- Anonymous March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(m+n) = O(2m) or O(2n) at the worst case
O(mlgn) or O(nlgm) is obviously greater than O(2m) or O(2n)

- Bala May 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

interesting discovery

- Anonymous August 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not necessary true.

Suppose m<n;

hash table, or maintain two index, O(2m)<O(m+n)<O(2n)
But the BST solution, it is O(mlogn)

O(2m)<=O(mlogn)<O(m+n), typically.

i.e., mlog n=m+n is the critical point.
when n is large, typically mlogn <m+n

- Anonymous August 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if m and n are of the same order of magnitude,
O(m+n) = O(2n) = O(n)
which is faster than
O(mlogn) = O(nlogn)

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

One of the most efficient algorithm to find out the intersection of two sorted arrays is to check for the following conditions which are as follows:
1. We will first check for the condition that if the element of the first array is greater than the element of the second array then, we will increment the index of the second array and the other condition will be just the opposite.
2. And the last condition will be that when the data of both are equal then we will increment the index of both the arrays.

Implementation:

void findintersection(int arr_1[], int m, int arr_2[], int n){
	int i = 0;
	int j = 0;
	while(i < m && j < n){
		if(arr_1[i] > arr_2[j])
			j++;
	        if(arr_1[i] < arr_2[j])
			i++;
		if(arr_1[i] == arr_2[j]){
			cout<<arr_1[i]<<" ";
			i++;
			j++;
		}
	}
}

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

@above: you have to store the an extra value to check for duplicates(assuming a number will occur only once in the intersection in case of duplicates on either side)

i = 0;
j = 0;
prev = a[0] < b[0] ? a[0] - 1 : b[0] - 1;
while (i < a.Length && j < b.Length) {
  if (a[i] == b[j] && prev != a[i])
  {
    print a[i];
    prev = a[i];
    i++;
    j++;
  }
  a[i] < b[j] ? i++ : j++;
}

- Thiyanesh January 22, 2010 | 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