Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

algo:
first list: 1 2 4 5 6
second list: 3 5 6 7 8 9

start from lowest of both the lists, in this case it is 1.
Have two variables for first list and second list.
var_first = 1
var_second = 3
output = min(var_first, var_second)
pick the second element from the list where output was found.In our case it is 2
var_first = 2
var_second = 3
keeps going....
var_first = 4
var_second = 3
var_first = 5
var_second = 5
whenever both variable are same put in the third list.
third list: 5 6

- aka April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Based on the above algo.

#include <stdio.h>

int main()
{
        int a[] = {1, 2, 3, 5, 6, 10, 11, 12};
        int b[] = {2, 3, 3, 6, 9, 10, 11};
        int c[100] = {0};
        int size, i, j, l, k, m, p;

        i = j = m = 0;
        size = (sizeof(a)/sizeof(a[0])) > (sizeof(b)/sizeof(b[0]))?(sizeof(a)/sizeof(a[0])):(sizeof(b)/sizeof(b[0]));
        memset(c, 0, sizeof(int)*100);
        for(;;) {
                if(a[i] > b[j]) {
                        j++;
                } else if(a[i] < b[j]) {
                        i++;
                } else if(a[i] == b[j]){
                        c[m++] = a[i];
                        i++;
                }
                if(i >= size || j >= size)
                        break;
        }

        for(i=0;i<m;i++)
                printf("%d\n", c[i]);
        return 0;
}

- aka April 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You are assuming elements to be integers.
What if they are some objects? how will you sort them?

- job_hungry April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@job_hungry : the question clearly says they are sorted arrays, that means the objects in it are comparable. If they are not comparable the best method I can think of is putting it into a Hash Set.

- CodePredator April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Create two hashsets using the arrays. The complexity would be n and m respectively. Now iterate through the set with smaller size and check if other set contains that element. The contains operation would be constant time operation yielding in overall complexity of n or m depending on the size of list. Ofcourse, you can do binary search but then complexity would be nlogm or mlogn.

- Prasad April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code:

public void intersectionOfSortedArrays(int a[], int b[]) {
		int al = a.length, bl = b.length;
		int c[] = new int[al + bl];
		int i = 0, j = 0, k = 0;

		while (i <= al - 1 && j <= bl - 1) {
			if (i < al && j < bl) {
				if (a[i] == b[j]) {
					if (k == 0 || c[k - 1] != a[i]) {
						c[k] = a[i];
						k++;
						i++;
						j++;
					} else {
						i++;
						j++;
					}
				} else if (a[i] < b[j]) {
					i++;
				} else
					j++;
			}
		}
		//Output
		for (int n = 0; n < k; n++)
			System.out.print(a[n] + " ");
	}

- Dhass April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a Tree set, add both the Arrays, which will Remove duplicates as well as maintains order

- Prashanth April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printCommon(int[] A, int[] B) {
	int firstIndex = 0;
	int secondndex = 0;
	while (firstIndex < A.length && secondndex < B.length)  {
		if(A[firstIndex] == B[secondndex]) {
			System.out.println(A[pointer[1]);
			firstIndex = increment(A, firstIndex);
			secondndex = increment(B, secondndex);
		}
		else if(A[firstIndex] < B[secondndex]) {
			firstIndex = increment(A, firstIndex);
		}
		else {
			secondndex = increment(B, secondndex);
		}
	}

}

public int increment(int[] A, int i) {
	int result = i;
	while(i < A.length) {
		if(A[result+1] == A[result]) result++;
		else break;	
	}
	return result + 1;
}

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

Correction:

public void printCommon(int[] A, int[] B) {
	int firstIndex = 0;
	int secondIndex = 0;
	while (firstIndex < A.length && secondIndex < B.length)  {
		if(A[firstIndex] == B[secondIndex]) {
			System.out.println(A[pointer[1]);
			firstIndex = increment(A, firstIndex);
			secondIndex = increment(B, secondIndex);
		}
		else if(A[firstIndex] < B[secondIndex]) {
			firstIndex = increment(A, firstIndex);
		}
		else {
			secondIndex = increment(B, secondIndex);
		}
	}

}

public int increment(int[] A, int i) {
	int result = i;
	while(result < A.length) {
		if(A[result+1] == A[result]) result++;
		else break;	
	}
	return result + 1;
}

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

public class CommonInteger {


public static void main(String[] args) {

int a[] = {1,2,3,4,5};
int b[] = {2,4,5,7,8,9,10};
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
for(int i=0;i<a.length;i++)
set1.add(a[i]);
for(int i=0;i<b.length;i++)
{
if(!set1.add(b[i]))
{
set2.add(b[i]);
}
}
Iterator<Integer> iter = set2.iterator();
while(iter.hasNext())
System.out.println(iter.next());
}

}

- Prashanth April 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we do a merging like operation except that we dont copy all elements into a new array, only those that are common ? This will take O(m+n) time ?

- Anonymous April 26, 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