Problem · Array

Join Sources with Lagged Destination Timestamps

Learn this problem
MediumPinterest logoPinterestFULLTIMEONSITE INTERVIEW

Problem statement

You are given two materialized tables. Each row of sourceRows is [source, destination, timestamp]. Each row of destinationRows is [destination, timestamp]. Timestamps are nonnegative integer seconds encoded as decimal strings.

A source row matches a destination row when their destination values are equal and the destination-row timestamp is zero, one, or two seconds after the source-row timestamp. Equivalently, if the timestamps are sourceTime and destinationTime, then 0 <= destinationTime - sourceTime <= 2.

Process destinationRows in input order. For each destination row, append the source of every matching source row in sourceRows input order. Preserve duplicate row occurrences. Rows with no match append nothing. Return the resulting list of source strings.

Function

matchLaggedSources(sourceRows: String[][], destinationRows: String[][]) → String[]

Examples

Example 1

sourceRows = [["A","X","10"],["B","X","12"],["C","Y","9"]]destinationRows = [["X","12"],["Y","10"]]return = ["A","B","C"]

For [X, 12], sources A and B lag by two and zero seconds. For [Y, 10], source C lags by one second.

Example 2

sourceRows = [["first","D","5"],["other","Q","6"],["second","D","6"],["first","D","5"]]destinationRows = [["D","7"],["D","4"],["D","7"]]return = ["first","second","first","first","second","first"]

The two identical source rows remain distinct occurrences. The middle destination row has no earlier source within two seconds. Repeating the first destination row repeats its three matches.

Example 3

sourceRows = [["late","A","9"],["edge","A","5"],["wrong","B","6"]]destinationRows = [["A","7"]]return = ["edge"]

edge is exactly two seconds earlier. late occurs after the destination row, and wrong has another destination.

Constraints

  • 1 <= sourceRows.length, destinationRows.length <= 20000
  • Every source row contains exactly three strings, and every destination row contains exactly two strings.
  • Source and destination strings are non-empty and contain only letters, digits, underscores, and hyphens.
  • Every timestamp is a decimal integer in [0, 10^9].
  • The returned list contains at most 200000 source occurrences.

More Pinterest problems

drafts saved locally
public String[] matchLaggedSources(String[][] sourceRows, String[][] destinationRows) {
    // Write your code here.
}
sourceRows[["A","X","10"],["B","X","12"],["C","Y","9"]]
destinationRows[["X","12"],["Y","10"]]
expected["A", "B", "C"]
checking account