The Minimum Common Value problem gives you two sorted integer arrays, nums1 and nums2. You need to find the smallest integer that appears in both arrays. If there is no common element, return -1.
This is a high-yield Minimum Common Value interview question at Microsoft and Google. It tests your knowledge of Two Pointers and Binary Search. It evaluates your ability to leverage the fact that the input arrays are already sorted, which is the key to an optimal or solution.
The Two Pointers interview pattern is usually preferred for its simplicity and linear time complexity.
i at the start of nums1 and j at the start of nums2.nums1[i] == nums2[j], you've found the minimum common value.nums1[i] < nums2[j], increment i to find a larger value.nums1[i] > nums2[j], increment j.nums1 = [1, 2, 3], nums2 = [2, 4].
i=0 (1), j=0 (2). 1 < 2, so i++.i=1 (2), j=0 (2). Match!
Minimum common value = 2.Always check if your inputs are sorted. If they are, Two Pointers should be your first thought. If one array is significantly smaller than the other, consider Binary Search instead, which might be even faster.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check If N and Its Double Exist | Easy | Solve | |
| Intersection of Two Arrays II | Easy | Solve | |
| Intersection of Two Arrays | Easy | Solve | |
| Merge Two 2D Arrays by Summing Values | Easy | Solve | |
| K-diff Pairs in an Array | Medium | Solve |