Return the Least Value Present in Both Sorted Arrays

Quick Overview

Find the smallest value present in both sorted arrays, allowing duplicates and returning -1 when there is no common value.

Return the Least Value Present in Both Sorted Arrays

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given positive integer arrays `nums1` and `nums2`, both sorted in nondecreasing order, return the smallest value occurring in both. One occurrence in each is sufficient. Return `-1` if there is no common value. ```cpp class Solution { public: int getCommon(vector<int>& nums1, vector<int>& nums2); }; ``` ### Examples **Example 1:** ```text Input: nums1 = [1,1,5,8], nums2 = [2,5,5] Output: 5 Explanation: Five is the first shared value; duplicate occurrences do not change the result. ``` **Example 2:** ```text Input: nums1 = [1,3], nums2 = [2,4] Output: -1 Explanation: The two arrays have no shared value. ``` ### Constraints - Each array has 1 through 100000 entries. - Values lie in [1,10^9]. - Both arrays are sorted nondecreasingly. ### Clarifying Questions - Are duplicate values allowed? Yes. - Do multiplicities have to match? No; one occurrence in each array is enough. - Are the inputs already sorted? Yes. - Is a value or an index returned? The common value. ```hint Check the boundary Check arrays whose common value appears several times in one array. ```

Overview: Find the smallest value present in both sorted arrays, allowing duplicates and returning -1 when there is no common value.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given positive integer arrays nums1 and nums2, both sorted in nondecreasing order, return the smallest value occurring in both. One occurrence in each is sufficient. Return -1 if there is no common value.

class Solution {
public:
    int getCommon(vector<int>& nums1, vector<int>& nums2);
};

Examples

Example 1:

Input: nums1 = [1,1,5,8], nums2 = [2,5,5]
Output: 5
Explanation: Five is the first shared value; duplicate occurrences do not change the result.

Example 2:

Input: nums1 = [1,3], nums2 = [2,4]
Output: -1
Explanation: The two arrays have no shared value.

Constraints

  • Each array has 1 through 100000 entries.
  • Values lie in [1,10^9].
  • Both arrays are sorted nondecreasingly.

Clarifying Questions Guidance

  • Are duplicate values allowed? Yes.
  • Do multiplicities have to match? No; one occurrence in each array is enough.
  • Are the inputs already sorted? Yes.
  • Is a value or an index returned? The common value.