Interview Question


Country: United States




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

If extra space is available, use hash map to store elements from B. Complexity - O(n)
If extra space is not available and arrays can be changed, sort arrays and then print elements from A not in B. Complexity - O(n logn)
If extra space is not available and arrays cannot be changed, use two loops to check if element of A is present in B. Complexity - O(n2).

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

For the second option i.e.
If extra space is not available and arrays can be changed, sort arrays and then print elements from A not in B. Complexity - O(n logn)

We can sort the arrays, but how do u print values which are not present in A, but not present in B? Whats the logic here?

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

“If extra space is available, use hash map to store elements from B.”
HashMap? key or value?

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

I'd like to change the wrong idea rooted deeply in your mind, I think you intended to use hashset, not hash map, and you think that if you use hash set, then the complexity will decreased, no, the implementation of hash set is including rb-tree, and the search time complexity of rb-tree is O(lgn), so the time complexity is also O(n*lgn). There is no algorithm of O(n) at all. And using hash set just can save your time and bring you convenience.

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

If we are allowed to use extra space:
1. Load items from array B into a collection (ex: ArrayList).
2. Loop through items in array A and in the loop check each item if it exists in the array list of B. If not found, print the item.

Efficiency = O(n), where n is number of items in array A.

Suggestions?

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

Manually checking whether an element in A is in the ArrayList is not constant time. So your solution is O(n^2), not O(n).

You might want to use a hash map to store elements in B. Then it is O(n), assuming the sizes of the two array are similar.

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

I am not sure what the interviewer wanted with this question but if python was an applicable language then after converting A and B into a set A-B would give you your result. Here is a sample code in python
>>> print set([1,3,4,6,9])-set([8,2,3,4])

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

Use hashmap

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

Let size of A is m and B is n
1) Put all values of B in std::set() . O(nlogn)
2) Check all values of A in the set(). one by one O(mlogn).
overall : O(max(nlogn,mlogn)).

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

Sort the smaller array - B
and then perform binary search for each element of array A
print not found numbers.

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

its not given that array B is small.
So sorting B would require nlog(n) complexity.
binary search on m iterations of A would require m*log(n) iterations.
So in total nlog(n)+m*log(n) complexity where m= size of A and n=size of B.
Good but not great

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

why would we consider arraylist when complexity matters? Coz anyway arraylist uses array internally which increases number of instructions.?

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

Let size of A is m and B is n
1) Put all values of B in std::set() . O(nlogn)
2) Check all values of A in the set() if its there dot contains() method O(1)
if its there call remove() i.e. O(1)
we have to check for all m elements so O(m).
now print all the elements of this set.

overall : O(nlogn)+O(n).= O(nlogn) it can be done

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

O(nlogn)+O(m)

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

Solution using sorting. Sort array b mlogm then for each element in a do a binary search nlogm, so total mlogm + nlogm

import java.util.ArrayList;
import java.util.Arrays;


public class ArrIntersection {

public static ArrayList<Integer> ANotB(int[] a,int[] b){
ArrayList<Integer> c = new ArrayList<Integer>();
Arrays.sort(b);
for (int i=0; i<a.length; ++i){
int idx = Arrays.binarySearch(b, a[i]);
if (idx < 0){
c.add(a[i]);
}
}
return c;
}

public static void main(String[] args){
int[] a = {1,4,3,6,9};
int[] b = {2,8,4,3};
ArrayList<Integer> c = ANotB(a,b);
for (Integer num : c){
System.out.println(num);
}
}
}

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

Solution with Hash, map b to hash O(m), search every item in a in the hash O(n), so total o(n)+o(m).

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class ArrIntersection {
	
	
	public static ArrayList<Integer> ANotBWithHash(int[] a,int[] b){
		Map<Integer, Boolean> bMap = new HashMap<Integer, Boolean>();
		for (int i=0; i<b.length; ++i){
			bMap.put(b[i], true);
		}
		ArrayList<Integer> c = new ArrayList<Integer>();
		for (int i=0; i<a.length; ++i){
			if (!bMap.containsKey(a[i])){
				c.add(a[i]);
			}
		}
		return c;
		
	}
	
	public static void main(String[] args){
		int[] a = {1,4,3,6,9};
		int[] b = {2,8,4,3};
		ArrayList<Integer> c = ANotBWithHash(a, b);
		for (Integer num : c){
			System.out.println(num);
		}
	}
}

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

public static void main(String args[]){

int[] A={1,4,3,6,9};
int[] B={2,8,4,3};

for(int i=0;i<A.length;i++){
int count = 0;
for(int j=0;j<B.length;j++){

if(A[i]!=B[j]){
count = count+1;
}
if(count==4){
System.out.print(A[i] + " ");
}
}
}

}

- sathishwaran January 27, 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