Google Interview Question for Software Engineer in Tests






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

ideone.com/5gElF
Found this.This should work if the query array doesnt have elements that repeat

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

Time O(n) Space O(1)

- sb August 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Didnt get what he is doing

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

This does not work. U are just trying to find if the given subsequence B is present in A or not and u are just report the first occurence of sequence B in A. If the input is say A=[2 1 3 4 2 3 4] and B=[2 3 4] your algorithm will o/p 2 1 3 4. While the correct answer is 2 3 4

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

<pre lang="c++" line="1" title="CodeMonkey92642" class="run-this">#include <iostream>

using namespace std;

bool minWindow(int N[], int n, int K[], int k, int Boundaries[]) {
int minWin=-1;
int* P = new int[k]; // All elements init to 0

P[0] = -1;
while(true)
{
for(int i=0;i<k;i++) {
if(i>0 && P[i] < P[i-1])
P[i] = P[i-1];
if(i==0 || P[i]==P[i-1]) {
do {
P[i]++;
if(P[i]>=n) {
delete[] P;
return minWin>0 ? true : false;
}
} while(N[P[i]]!=K[i]);
}
}
int win = P[k-1] - P[0] + 1;
if(minWin==-1 || win<minWin) {
Boundaries[0] = P[0];
Boundaries[1] = P[k-1];
minWin = win;
}
}
}

int main() {
int N[] = {23, 46, 24, 16, 16, 29, 35, 2,77,29, 35, 77, 29, 2 };
int K[] = {16,2,77,29};
int Boundaries[2];
bool success = minWindow(N, sizeof(N)/sizeof(int), K, sizeof(K)/sizeof(int), Boundaries);
if(success)
cout << "start: " << Boundaries[0] << ", end: " << Boundaries[1] << endl;
else
cout << "No such window" << endl;
return 0;
}</pre><pre title="CodeMonkey92642" input="yes">
</pre>

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

can u tell me what is the run time of this algorithm.I've a algorithm running in O(n*m) using dynamic programming

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

The LCS thing won't work here as LCS will generate only one of the window having all query terms in increasing order, not necessarily the shortest window.

The last code is a nice one :) Works fine and the most optimal one. I also made a similar code during my interview :)

- Ankul September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A little addition, in last code by Anonymous, there may be large number of useless iterations searching for a particular query term if the size of input array is too large and the only window is present at the end of the input array. In such a case, preparing a term-position matrix would be a better idea to avoid useless iterations. The term-position matrix will store for each query term, occurences of the term in the input array in increasing order. Thus, the search space will become shorter (only the positions where the query terms are present).

- Ankul September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Input [1 9 3 4 12 13 9 12 21 ]
query Array [9 12 21]

1. Find the index of each element in the query Array
9 -- 2,7
12 -- 5,8
21 -- 9

2. Calculate the distance, such that all elements are included. The following distances are possible
2->5->9 = 3+4 = 7
2->8->9 = 6+1 = 7
7->8->9 = 1+1 = 2
(Note :7 > 5, so the path 7->5->9 is not acceptable. because order has to be maintained)
3. Output the window between (7,9)

May be someone can comment on the complexity.

- V September 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Order is O(n*k). Not a good solution. :(

- V September 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea man, lot space for improvements thou. What we have is sorted arrays of positions we have to search. Now we have to find smallest range of integers in these arrays, so we do it in this manner: Iterate first array from last element to first, and for every index encountered, search element more than this one in next array using binary search. continue this process for all remaining arrays.

Using this search pattern we guarantee that first found solution is the smallest one, i.e. the one we're seeking.

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

My wrong, We need to consider all elements from first array. Rest seems correct. comments appreciated :)

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

could be O(m). since search can be done in O(1) with hash map.

- q September 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

at last one thing i want to add store occurrence of each of the digit of sub array and store it in 2-d matrix and check along the each column....like
9 -- 2,7
12 -- 5,8
21 -- 9,9 << repeat the value >>
first column = 9-2 = 7;
second column = 9-7 = 2;
so second column is minimum...

- nagpal_dce September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about using linked list?
1.Create a linked list out of array 1
2.search for the first element of array 2 in linked list
3.Compare remaining elements from array 2
4.At any point if there is mismatch and there more elements in array 2 stop comparing
5.Find the next match of first element from array 2 in linked list (remember the location of first match and look for the next match after than).
6.If number of elements in linked list from the match is not equal to or greater than the number of elements of array two break.

Create linked list O(n), find first element O(n), compare elements in array 2 O(m)

2O(n)+O(m)

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

bool FindPattern( int input[], int inSize, int query[], int qSize)
{
int k = 0;
int j=0;
bool matchfound = false;

while ( k+qSize <= insize)
{
while ( input[k] != query[j])
k++;

for( int i = k, j=0; j< qSize ;i++,j++)
{
if( input[i] != query[j])
break;

}
if(j== qSize)
{
matchfound = true;
break;
}
j=0;
k = i;
}

return matchfound;

}

- raz September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u r wrong.

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

#include<stdio.h>
#include<conio.h>
int a[]={1,9,3,4,12,13,9,2,12,21,7};
int alen=11;
int b[]={9,12,21};
int blen=3;
int found[11];
int length[11];

void commonwindow()
{
int i,m,n,tt;
int count=0,k=0;
for(i=0;i<alen;i++)
{
if(a[i]==b[0])
{
count++;
found[k++]=i;

}
}
k=0;

while(k<count)
{
m=found[k];
n=0;
tt=0;
while(m<alen )
{
if(a[m]==b[n])
{
n++;
}
if(n==blen)
break;
m++;
tt++;
}
if(n==blen)
{
length[k]=tt;
}
else
length[k]=0;


k++;
}

for(i=0;i<count;i++)
{
printf("\nPosition and length:");
printf("%d,",found[i]);
printf("%d",length[i]);
}

}

void main()
{
clrscr();
commonwindow();

getch();

}

- Elamaran V September 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The final Found array contains the position of posibble common windows in A array and the corresponding Length array contains its length.Length zero denotes that it is not a common window.We can get the minimum window using these arrays.....

Space complexity:2O(n)
Time Complexity:O(n^2) in worst case

Can this be done better????Try for many test cases

- Elamaran V September 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
suppose you have array as a1,a2,...an and query array as b1,b2,...,bk now make a array P as p1,p2,...pk initialize all elements with -1 now {{{ i=0;j=0; minw = n; >>>> start searching in A for b[j] while(true){ if i==n and j<k then stop( break); if( A[i] == B[j]) { if( j==k ) { if( (P[k]-P[0] + 1) < minw) then minW = P[k]-P[0]+1; i = P[0]+1; j=0; }else{ P[j++] = i; if( P[j] > i) then i = P[j] and continue; } } i++; } cout << "minw"; }} Tell me if its WRONG ...!! - Aditya September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

* Find all position of all items of array2 : O(m+n)
* implement restricted merge sort ? O(m+n)

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

public class main {
    public static void main(String args[])
    {
    int z=0;
int flag=0;
int[] oaray=new int[15];
int[] qaray={4,8,10};
int[] subindex = new int[5];
for(int i=0;i<12;i++)
    oaray[i]=i+1;

for(int i=0;i<12;i++)
{
if(oaray[i]==qaray[0])
{
flag=1;
subindex[z++]=i;
for(int j=1;j<3;)
{
if(oaray[++i]==qaray[j])
{
subindex[z++]=i;
j++;
}
}
}
if(flag==1)
break;
}
for(int a=0;a<3;a++)
    System.out.println(subindex[a]);
    }
}

- meena-kadamalai September 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let the data array be { 1, 2, 5, 4, 1,5, 2, 3, 5 }
Query array = { 1, 2, 5}
Create a array pos = { -1 , -1, -1 }
min_window_start=0,min_window_end=n-1, min_len=n;

Search the data array from n-1 to 0 and see whether you find any matching
in query array, if there is matching update the index in pos array,
if, it maintains the increasing sequence of positions. If all positions are filled
find the distances between 1st and last element, and check whether it is better than
earlier min_len, if it is update the new value for all the min_* variable.

When you reach zero, you got the minimum window, in the min array.

- Bean November 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is exactly the same as a substring search. Just use KMP algorithm.

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

public static void containsAllInSeq(char a[] , char b[]){
int n = a.length;
int m = b.length;
int j, k;
for(int i = 0; i <= n-m; i++){
if(a[i] == b[0]){
j = i+1;
k = 1;
while((j < i+m) && (a[j] == b[k])){
j++; k++;
}
if(k==m){
System.out.println("SubString found at index "+i);
return;
}
}
}
System.out.println("Not Found");
}

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

i have a nLog(n) solution

- Yu February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nlog(n) solution
use string as example
arr1: a a * b * * c * * * a * * b * c
location: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
arr2:abc
hash arr1, Hash<list<int>> hash
hash[arr[i]]->push_back(i);
so the hash become:
a: 1,2,11
b: 4,14
c: 7,16
begin=0
end=0;
while(true){
//expand the window by move end to right
then for j=1:sizeof(arr2)
nextbegin=find the first element in hash[arr2[j]] which is >=begin
if can not find ,then break;
begin=nextbegin+1;
}
end=begin-1;//find one match ,but the left may move right to reduce the window

//reduce the window by move begin to right
tmp=end
for j=sizeof(arr2)-1:1
tmp=find the first element in hash[arr2[j]] which is <tmp
}
begin=tmp;

update minLength use [begin,end];
begin++;
}
every match use m*log(n)*2 time, if arr2 is not like aaaaabbbbb(continues element,which need to optimazed by match several repeated elements), there are at most n/m matches, so n/m * mlog(n)=nlog(n)

- Yu February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

update:
for j=sizeof(arr2)-1:1
tmp=find the first element in hash[arr2[j]] which is > tmp
}

- Yu February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nlog(n) solution
use string as example
arr1: 	       a a * b * * c * *  *  a  *  *  b  *  c
location:      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
arr2:abc
hash arr1, Hash<list<int>> hash
hash[arr[i]]->push_back(i);
so the hash become:
a: 1,2,11
b: 4,14
c: 7,16
begin=0
end=0;
while(true){
	//expand the window by move end to right
	then for j=1:sizeof(arr2)
		nextbegin=find the first element in hash[arr2[j]] which is >=begin
		if can not find ,then break;
		begin=nextbegin+1;
	}
	end=begin-1;//find one match ,but the left may move right to reduce the window

	//reduce the window by move begin to right
	tmp=end
	for j=sizeof(arr2)-1:1
		tmp=find the first element in hash[arr2[j]] which is <tmp
	}
	begin=tmp;
	
	update minLength use [begin,end];
	begin++;
}
every match use m*log(n)*2 time, if arr2 is not like aaaaabbbbb(continues element,which need to optimazed by match several repeated elements), there are at most n/m matches, so n/m * mlog(n)=nlog(n)

- Yu February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

m =n and n = k here. Can this be done better?

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

sort the query array O(klogk). from left to find the array in query, and fron right to find the array in query. O(nlogk).
We also can put the array in hash, and the find will just take O(n)

- remlostime November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public int findWindow (m) {

  public static int start = end = 0;
  public static int bIndex = 1;

  // small problem
  if(m < k) {
	return 0;
  }

  // sub-problem
  smallestWindow = findWindow (m-1);

  // find out next steps from the sub-problem
  if(a[m] == b[bIndex]) {
    if(bIndex == 1) {
	  start = m;
	}
	else if(bIndex == k) {
	  end = m;
	  bIndex = 0;
	}
	bIndex++;
  }
  if(start < end) {
    if(end - start + 1 < smallestWindow) {
	  smallestWindow = end - start + 1;
	}
  }

  return smallestWindow;
}

- ravia September 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

any comments on this solution ?

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

Am I mistaken or is the program supposed to run when I hit the button? I get some kind of error though. But cool ... I didn't know you could execute code here.

- Bullocks September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.


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