FastPrepSmallest Common Integer in Sorted Lists

Smallest Common Integer in Sorted Lists

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

You are given lists, a collection of integer lists. Every inner list is sorted in nondecreasing order.

Return the smallest integer that appears in every inner list. Repeated copies within one list count as one presence. If no integer appears in every list, return -1.

Function

smallestCommonInteger(lists: int[][]) → int

Examples

Example 1

lists = [[1,2,3,4],[0,2,4],[2,5,9]]return = 2

The value 2 appears in all three lists, and no smaller value does.

Example 2

lists = [[1,2],[3,4]]return = -1

The two lists have no common integer.

Example 3

lists = [[0,0,1],[0,2],[0,0,3]]return = 0

Duplicate copies do not matter; 0 is present in every list.

Constraints

  • 1 <= lists.length <= 100.
  • 1 <= lists[i].length, and the total number of integers is at most 200000.
  • 0 <= lists[i][j] <= 1000000000.
  • Every inner list is sorted in nondecreasing order.

More Motive problems

See Motive hiring insights
public int smallestCommonInteger(int[][] lists) {
    // write your code here
}
lists[[1,2,3,4],[0,2,4],[2,5,9]]
expected2
Checking account…