FastPrepFind Duplicate Element Pairs

Find Duplicate Element Pairs

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given ids.length elements. Element i has the unique identifier ids[i] and exactly three property values in properties[i].

Two distinct elements form a duplicate pair when they share at least one identical property value anywhere among their three properties. Duplicate pairing is direct: if element a matches b and b matches c, that chain alone does not make a and c a duplicate pair.

Return every duplicate identifier pair exactly once. Within each pair, place the earlier input element first. Order the returned pairs by the earlier input index and then by the later input index.

Property comparison is exact and case-sensitive. Repeating one property value inside an element does not duplicate an output pair.

Function

findDuplicatePairs(ids: String[], properties: String[][]) → String[][]

Examples

Example 1

ids = ["id1","id2","id3"]properties = [["p1","p2","p3"],["p1","p6","p5"],["p3","p7","p8"]]return = [["id1","id2"],["id1","id3"]]

id1 shares p1 with id2 and p3 with id3. The remaining pair shares no property.

Example 2

ids = ["a","b","c"]properties = [["x","p","q"],["x","y","r"],["y","s","t"]]return = [["a","b"],["b","c"]]

a matches b through x, and b matches c through y. The chain does not create an a-c pair.

Example 3

ids = ["left","right"]properties = [["a","b","c"],["d","e","f"]]return = []

The two elements share no property value, so there are no duplicate pairs.

Constraints

  • 1 <= ids.length = properties.length <= 2000.
  • Every identifier is unique.
  • properties[i].length = 3.
  • Identifiers and property values are non-empty strings of at most 30 printable ASCII characters.
  • Property comparison is case-sensitive.

More Google problems

See Google hiring insights
public String[][] findDuplicatePairs(String[] ids, String[][] properties) {
    // Return every directly duplicate pair in input-index order.
}
ids["id1","id2","id3"]
properties[["p1","p2","p3"],["p1","p6","p5"],["p3","p7","p8"]]
expected[["id1", "id2"], ["id1", "id3"]]
Checking account…