Interview Question


Country: United States




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

this question has solutions now. and the time complexity is max(m*logm , n*logn). At first you should sort one of the array, then use a 'for' loop for the other array, using binary search, to every element, it will use logm or logn time. and the space complexity is o(1). And your question is the simple one, the other one is can you find all the common elements in two arrays.

- yingsun1228 January 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about using hashing to store the first array? Thereafter, for every element in second array, look up to find it exists is O(1)

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

You will have to use a map<char,int> and push the longer array into the map keeping track the number of characters pushed in the map. Then you go through the shorter array and decrement the number of characters if it exist. If at any point you hit a negative value of if the character does not exist in the array you return false.

- Piyush Gadigone January 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ yingsun1228, Your solution wont work with duplicate elements. Here is one example where your solution will report "IDENTICAL ARRAYS"

Array 1 = [1,2,3]
Array 2 = [1,1,1]

- Vikas January 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rashmi, Your solution won't work for this input:

Array 1 = [1,2,3]
Array 2 = [1,1,1]

- Vikas January 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you give us your codes? I did not quite understood what you mean.

- yingsun1228 January 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Sort the list, search with a bool[]?

public boolean sharedElem(int[] aList,int[] bList){
		Arrays.sort(aList); Arrays.sort(bList);
		
		int maxElem = aList[aList.length-1] > bList[bList.length-1] 
				? aList[aList.length-1] : bList[bList.length-1];
		
		boolean[] hasElm = new boolean[maxElem+1];
		
		Arrays.fill(hasElm,false);
		for(int i = 0; i < aList.length;i++){
			hasElm[aList[i]] = true;	
		}
		for(int j = 0; j < bList.length; j++){
			if(hasElm[bList[j]])
				return true;
		}
		return false;
	}

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

The complexity should be max(mlogn,nlogn)? If there are duplicates in the arrays, this would not work.

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

we use hash, but with following data structure

struct data_item
{
    bool present;
    int count;
   data_item()
   {
       present = false;
       count = 0;
   }
};
hash_map<int , data_item> *comp;

where int is item, and structure data_item tells us about weather an item is present or not, if it present then how many time it is,
Now while comparing we always check

for each input
    if(comp(item)->present)
        comp(item)->count--;
        and go to next input
   else
       output not same
       return;
output same;
return;

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

Algorithm:
I. iterates array1 and add into hash map <value, occurrence>;
E.g. for 3rd and 6th index (8), hash value will be <8, 1> and <8, 2>
2 .iterate array2 and check whether that value exist or not, if yes then reduce the occurrence by 1 .if not, return false;
3. Iterate the whole hash map if the all values occurrence is 0 then return true else return false;

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

The greatest value of the two arrays is 8, thus a byte is sufficient to represent the existence/non-existence of all the values in either array.
To verify whether two arrays contain a set of value, mask the two corresponding bytes and & them together

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

This question has very elegant solution. Take one variable var = 0. Take XOR of all the elements in first array and second array with var. If the final answer is zero than both the arrays contain same elements, otherwise not.

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

Would your solution work for:

Array 1 = [1,1]
Array 2 = [2,2]

If I understood your solution correctly, final answer would be 0 but the arrays do not have any common elements.

- Vikas January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

two solution :
1 : sort both, O(max(nlogn,mlogm)); where n and m are the size of arrays;
and then compare with avoiding duplicate's, O(n+m);
total time complexity : O(max(nlogn,mlogm))
2 : use hashing in following way has_map<int , bool>, where int represent data item, and bool variable represent weather an data item is present or not ?
we start reading array one, and if an element present more then once, we don't worry, the bool variable is set to true, Now we go to second array, and we check weather there exist an element in this array whose bool variable is not set, if we are able to find one, print not true, if not, then do the same by first taking second array to populate map and check first array for validation ,if we found one whose bool variable is not set true, we print not true, else we print true
time complexity : O(max(m,n));

- sonesh January 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{


int arr1[] = {4,5,6,3,1,2};

int arr2[] = {2,1,3,4,5,6};

int len1 = sizeof(arr1) / sizeof(int);

int len2 = sizeof(arr2) / sizeof(int);

int num = 0;

for(int i = 0; i < len1; i++){

num ^= arr1[i];
}

for(int i = 0; i < len2 ; i++){

num ^= arr2[i];
}

if(!num)
printf("ARRAYS ARE SAME");
else
printf("ARRAYS DIFFER");

}

- BalaMurali krishnan January 22, 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