Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.
Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4] Output: 2 Explanation: The smallest element common to both arrays is 2, so we return 2.
Example 2:
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5] Output: 2 Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
Constraints:
1 <= nums1.length, nums2.length <= 1051 <= nums1[i], nums2[j] <= 109nums1 and nums2 are sorted in non-decreasing order.Problem summary: Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1. Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers · Binary Search
[1,2,3] [2,4]
[1,2,3,6] [2,3,4,5]
intersection-of-two-arrays)intersection-of-two-arrays-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2540: Minimum Common Value
class Solution {
public int getCommon(int[] nums1, int[] nums2) {
int m = nums1.length, n = nums2.length;
for (int i = 0, j = 0; i < m && j < n;) {
if (nums1[i] == nums2[j]) {
return nums1[i];
}
if (nums1[i] < nums2[j]) {
++i;
} else {
++j;
}
}
return -1;
}
}
// Accepted solution for LeetCode #2540: Minimum Common Value
func getCommon(nums1 []int, nums2 []int) int {
m, n := len(nums1), len(nums2)
for i, j := 0, 0; i < m && j < n; {
if nums1[i] == nums2[j] {
return nums1[i]
}
if nums1[i] < nums2[j] {
i++
} else {
j++
}
}
return -1
}
# Accepted solution for LeetCode #2540: Minimum Common Value
class Solution:
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
i = j = 0
m, n = len(nums1), len(nums2)
while i < m and j < n:
if nums1[i] == nums2[j]:
return nums1[i]
if nums1[i] < nums2[j]:
i += 1
else:
j += 1
return -1
// Accepted solution for LeetCode #2540: Minimum Common Value
impl Solution {
pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let m = nums1.len();
let n = nums2.len();
let mut i = 0;
let mut j = 0;
while i < m && j < n {
if nums1[i] == nums2[j] {
return nums1[i];
}
if nums1[i] < nums2[j] {
i += 1;
} else {
j += 1;
}
}
-1
}
}
// Accepted solution for LeetCode #2540: Minimum Common Value
function getCommon(nums1: number[], nums2: number[]): number {
const m = nums1.length;
const n = nums2.length;
let i = 0;
let j = 0;
while (i < m && j < n) {
if (nums1[i] === nums2[j]) {
return nums1[i];
}
if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.