Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
My solution easily fulfiled the first 2 requirements, but have no idea about the third one.
If only nums2 cannot fit in memory, put all elements of nums1 into a HashMap, read chunks of array that fit into the memory, and record the intersections.
If both nums1 and nums2 are so huge that neither fit into the memory, sort them individually (external sort), then read 2 elements from each array at a time in memory, record intersections
And an other ideas:
I think the goal of this question is to test whether the interview understands some of the data engineering techniques. From a data engineer’s perspective, basically there are three ideas to solve the question:
Store the two strings in distributed system(whether self designed or not), then using MapReduce technique to solve the problem;
Processing the Strings by chunk, which fits the memory, then deal with each chunk of data at a time;
Processing the Strings by streaming, then check.
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
int i=0,j=0;
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList<Integer> list = new ArrayList<>();
while(true){
if(i==nums1.length||j==nums2.length) break;
if(nums1[i]<nums2[j]) i++;
else if(nums1[i]>nums2[j]) j++;
else{
list.add(nums1[i]);
i++;j++;
}
}
int[] res = new int[list.size()];
for(int m=0;m<res.length;m++) res[m]=list.get(m).intValue();
return res;
}
}