Google Interview Question for Software Engineer / Developers






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

just use dp.......KMP will do fine in o(n) time complexity

- Anonymous February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1,2,3,4,5
2,3
true
1,2,3,4,5
2,4
false. why this is false? first array has 2,4 right?

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

2 and 4 are not consecutive.

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

hash small array and look for big array elements in hash table one by one.
n -big array
m - small array
time - O(n+m)
space - O(m)

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

dono if its a a good idea why do we hash when we have to pattern match... i mean extra space and still u need to know if the hash keys are in an order...u might think of using linkedhashmap

- Anonymous February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good Solution.

- siva.sai.2020 March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If array contains duplicate elements then we need to take extra care.

Example:

A[] = {1,1,1,1,1,1,1}
B[] = {1,2,3}

here big array is A[] with respect number elements.

But if consider distinct elements, then B[] is big array

- siva.sai.2020 March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A1 [1...n1]
A2 [1...n2]
int OneContainInOther(A1,l1, r1, A2,l2, r2) {
int index ;
if (r1 > l1 || r2 > l2)
return 0 ;
if (BinSearch(A2[r2], &index))
return (A1, l1, index - 1, A2, l2, r2 -1) ;
return 0;
}

Sort both A1 and A2
if (n1 > n2) // Check for A1 contains A2
OneContainInOther(A1, 1, n1, A2, 1, n2) ;
else // Check for A2 contains A1
OneContainInOther(A2, 1, n2, A1, 1, n1) ;

- Akd February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why sort?
why binary search?
the request is totally depends on the order

- ana February 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int contain(int a[],int b[])
{
  
  for(i=0;i<a.length();i++)
  {
  for(j =0;j<b.length();j++)
   {
     if(a[i]==b[j])
     {
         return  comp(i,j,a,b);
     }
     return 0;


}


int comp(int i,int j,int a[],int b[])
{
   for(k=i;k<j;k++)
   {
     if(a[i]!=b[i])  
     {

       return 0;

     }
      


   }

 return 1;


}

- dushy February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct code

int contain(int a[],int b[])
{

for(i=0;i<a.length();i++)
{
for(j =0;j<b.length();j++)
{
if(a[i]==b[j])
{
return comp(i,j,a,b);
}
return 0;


}


int comp(int i,int j,int a[],int b[])
{
for(k=i;k<j;k++)
{
if(a[k]!=b[k])
{


return 0;


}


}


return 1;


}

- dushy February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsArrayConatain(int [] a, int [] b)
{
for(int i = 0; i< n; i++)
{
for(int j=0; j<m; j++)
{
if(a[i] == b[j])
{
for(int k = j + 1 ; k < m ; j++)
{
if(a[k] != b[k])
{
return false;
}
}
return true;
}
}
}
return false;
}

- davin February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsArrayConatain(int [] a, int [] b)
 {
	for(int i = 0; i< n; i++)
	{
		for(int j=0; j<m; j++)
		{
			if(a[i] == b[j])
			{
				for(int k = j + 1 ; k < m ; j++)
				{
					if(a[k] != b[k])
					{		
						return false;	
					}
				}
				return true;
			}
		}
	}
	return false;
 }

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

But the array may be not have unique elements ..
i.e array1[]={1,2,1,4,5} , array2[]={1,4,5}
in your code this return false ... but the correct is to return true ..

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

You two are the smartest programs on earth!
I'm surprised seeing such O(n^3) time solution from google job seakers!

- @dushy & @davin February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just do a Boyer-Moore search or a KMP search. I personally prefer the former, it is such an elegant algorithm.

- vinod. February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
boolean isArray(int[] a, int[]b){ if(a==null || b== null){ throw new NullPointerException(); } if(a.length == 0 && b.length==0){ return true; } if(a.length < b.length){ int[] temp = a; a = b; b = temp; } boolean found = true; for(int i = 0; i<a.length; i++){ if(a[i] == b[0]){ int index = i; for(int j = 0; j<b.length; j++){ if(b[j] == a[index+j]){ found = false; break; } } if(found){ return found; } } } return false; } } }}}{{{ boolean isArray(int[] a, int[]b){ if(a==null || b== null){ throw new NullPointerException(); } if(a.length == 0 && b.length==0){ return true; } if(a.length < b.length){ int[] temp = a; a = b; b = temp; } boolean found = true; for(int i = 0; i<a.length; i++){ if(a[i] == b[0]){ int index = i; for(int j = 0; j<b.length; j++){ if(b[j] == a[index+j]){ found = false; break; } } if(found){ return found; } } } return false; } - Ayanjyoti February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class array_match {

/**
* @param args
*/

public static int check(int[] a,int[] b)
{
int j=0;
for (int i = 0; (i < a.length) && (j < b.length);i++)
{
if(a[i] == b[j])
{
j++;

}
else
{
j=0;

}
}

if (j == (b.length))
return 1;
else return 0;
}

public static void main(String[] args) {
// TODO Auto-generated method stub


int[] a={1,2,3,4,5};
int[] b={2,3};
int[] c={2,5};

if (check(a,b)==1)
{
System.out.println("a and b Match");

}
else
System.out.println("a and b No Match");


if (check(a,c)==1)
{
System.out.println("a and c Match");

}
else
System.out.println("a and c No Match");





}

}

- Nohsib February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

suffix array

- anson627 February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A---> bigger array
B---> smaller array
n = length.A
m = length.B
for j=0:n-1
i =0
key = B[i]
if(key == A[j])
i++
if(i == m)
search = true
else if(j = n && i != m)
search = false
end

- Arpit February 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* function for a method that takes two arrays and returns true if one array is contained in the other.
* @param container int[]
* @param contained int[]
* @return boolean
*/
public static boolean containsArray(int []container, int[] contained) {
int containerLenth = container.length;
int containedLength = contained.length;
if(containerLenth< containedLength){
return false;
}
int index = Arrays.binarySearch(container, contained[0]);
if(index != -1){
if(containerLenth - index < containedLength){
return false;
} else {
for (int i=0;i<containedLength;i++) {
if( container[index+i]!= contained[i]){
return false;
}
}
return true;
}
}

return false;
}

- Rajeev Krishna Singh March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

m = length of small array
n = length of larger array
sort smaller array in m*lg(m)
binary search every element in larger array in n*lg(m) operations.
total complexity: n*lg(m)

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

From the sample, it is just a string search.

- yinjike March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An O9n) algorithm :
public static bool isSubset(int[] a, int[] b)
{
int chk =0;
int v=-1;
int ct = 0;
for ( int i=0;i< b.Length; i++)
{
chk |= (1<< b[i]);
}

for( int i=0;i< a.Length;i++)
{
if ((chk & (1 << a[i])) > 0)
{
//kick off...///
if (v == -1)
{
v = i; ct++;
}
else if ((v > 0) && (i == v + 1))
{
v = i; ct++;
}

}
else if (ct == b.Length)
{
break;
}
else
{
v = -1; ct = 0;
}
}
if (v > 0) { return true; }
else { return false; }

}
please let me know if there are any errors....
thankyou!!

- kk September 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming the n is much bigger than n
1. Scan m to find 1 element that is not repetitive O(m).
2. If found them binary search that search element in array n, O(long n)
3. If found match the remaining patter from m around that elment O(m)
so for this case where there is at least 1 non repetitive in m complexity = O(2m) + O(log n)
If there is no non-repetitve element the it will be O(m.log n)

- Satish November 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming the n is much bigger than m
1. Scan m to find 1 element that is not repetitive O(m).
2. If found them binary search that search element in array n, O(long n)
3. If found match the remaining patter from m around that elment O(m)
so for this case where there is at least 1 non repetitive in m complexity = O(2m) + O(log n)
If there is no non-repetitve element the it will be O(m.log n)

- Satish November 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

modify KMP and you will get it. O(m+n).

- Anonymous January 15, 2013 | 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