Google Interview Question for Software Engineer in Tests






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

Set<Integer> intersection(int a[], int[] b) {

		Set<Integer> fir_set = new HashSet<Integer>();
		Set<Integer> intersection = new HashSet<Integer>();
		
		for (int i : a) {
			if (fir_set.contains(i)) {
				continue;
			}
			fir_set.add(i);
		}

		for (int j : b) {
			if (fir_set.contains(j))
			{
				intersection.add(j);
			}
			
		}
		return intersection;
	}

This uses Set data structure.

- sunny July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

void interset(int * arr1, int sz1, int * arr2, int sz2) 
{
  if (!((sz1 > 0) && (sz2 > 0))) return;
  
  int *p1 = arr1;
  int *p2 = arr2;
  
  while ( (p1 <= (arr1+sz1)) && (p2 <= (arr2+sz2)))
  {
    if (*p1 == *p2) {
      printf("%d ", *p1);
      p1++; 
      p2++;
    } else if (*p1 > *p2) {
      p2++;
    } else {
      p1++;
    }
  }
}

- hunt September 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Do a merge sort?

- Lei September 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

while(i<n && j<m){
if(a[i] < b[j]) i++;
else if(b[j] < a[i]) j++;
else {System.out.println(a[i]); i++; j++;}
}

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

@hunt: your answer is incomplete, though you're trying to find the matching elements/intersection.

@morpheus: question is not clear
#1 . does the intersection part appears contiguously? or partly here partly there
give a proper example.

- klpd September 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hunt's answer seems to on the correct lines.
interviewer added later that no dupes should be printed.
also asked me to list the (unit) testcases for the function.

- morpheus September 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hunt's approach seems correct. As far as Duplicate elimination goes, a slight modification to hunts algorithm to store the intersected element before printing and on the next match, check if the previously stored element is not the same as the current matching element detected. Any opinions ?

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

@amit, you're correct. :) that'll work....but how about this...




int intersect(int a[],int uba,int b[],int ubb,int c[]) //c is atleast uba+ubb...returns ubc
{
int lba = 0, lbb = 0, lbc = 0;

while( (lba < uba) && (lbb < ubb))
{
if( a[lba] == b[lbb])
{
int t = a[lba];
while(a[lba] == t) lba++;
while(b[lbb] == t) lbb++;
c[lbc++] = t;
}else
if( a[lba] < b[lbb])
lba++;
else
lbb++;

}
return lbc;
}

- EveryoneLovesMicrosoft October 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can't your 2 inside while loops go out of bound?

- Super Google Fanboy August 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a HashMap

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

public void printIndex(int[] A, int[] B){
	int a = 0;
	int b = 0;
	int lengthA = A.length;
	int lengthB = b.length;
	while(a < lengthA && b <lengthB) {
		if (A[a] == B[b]) {
			System.out.println(A[a]);
			a++;
			b++;
		}
		else if (A[a] < B[b]) {
			a++;
		}
		else {
			b++;
		}
	}
}

- Anonymous April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printIndex(int[] A, int[] B){
	int a = 0;
	int b = 0;
	int lengthA = A.length;
	int lengthB = b.length;
	while(a < lengthA && b <lengthB) {
		if (A[a] == B[b]) {
			System.out.println(A[a]);
			a++;
			b++;
		}
		else if (A[a] < B[b]) {
			a++;
		}
		else {
			b++;
		}
	}
}

- srikantaggarwal April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For solving the above algorithm we will need to do a merge sort operation, and we will compare the elements of the two arrays if the first element of the first array is less than the first element of the second array then increment the index of the first array because there may the next element which intersects with the first array and similarly increment the index of the second array when the element of the second array is less than the first array and when both the array elements are equal then print the element and increment both the array indexes.

- swapnilkant11 July 24, 2019 | 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