As-Of Join
Updated
Problem statement
You are given two tables, leftRows and rightRows, each sorted by timestamp. Each row is an integer array. Its first value is the timestamp. The rest of the row holds the data, which we call value columns.
matchColumns lists the value column indices that must match between the two tables. For each left row, find the last right row that passes both checks:
- Its timestamp is less than or equal to the left row's timestamp.
- At every index in
matchColumns, its value equals the left row's value.
Keep the whole left row, then add that right row's value columns to the end. Do not copy the right row's timestamp. If several matching right rows share the best timestamp, use the one that comes last in rightRows. If no right row passes both checks, add -1 once for each value column of the right table.
If matchColumns is empty, pick rows by timestamp alone. No values need to match.
Return the joined rows in the same order as the left table.
Function
asOfJoin(leftRows: int[][], rightRows: int[][], matchColumns: int[]) → int[][]Examples
Example 1
leftRows = [[1,10],[4,20],[6,30]]rightRows = [[2,100,101],[4,200,201],[5,300,301]]matchColumns = []return = [[1,10,-1,-1],[4,20,200,201],[6,30,300,301]]No columns need to match, so only timestamps count. Left timestamp 1 is earlier than every right row, so both right value columns get the fill value for a missing match. Left timestamp 4 joins the right row at 4. Left timestamp 6 joins the right row at 5, the latest one not after it.
Example 2
leftRows = [[2,1],[3,2]]rightRows = [[2,7],[2,8],[5,9]]matchColumns = []return = [[2,1,8],[3,2,8]]For each left row, both right rows at timestamp 2 qualify, and the last right row is too late. On a timestamp tie, the later row in the list wins, so both left rows get the value 8.
Example 3
leftRows = [[10,4,5]]rightRows = [[1,6],[9,7]]matchColumns = []return = [[10,4,5,7]]No columns need to match. Both right rows are at or before timestamp 10. The latest one is at 9, so its value 7 is added to the end of the left row.
Example 4
leftRows = [[2,7],[3,9],[4,7]]rightRows = [[1,7,100],[2,9,200],[3,7,300]]matchColumns = [1]return = [[2,7,7,100],[3,9,9,200],[4,7,7,300]]Rows must match on the one listed value column, which we call the key. The first left row cannot use the second right row: that row is newer, but its key is different. So the first left row takes the first right row, which has the same key. The second left row has the same key as the second right row, which is not later than it, so it takes that row. The third left row is later than every right row. The newest right row is the third one, and its key matches, so the third left row takes it.
Example 5
leftRows = [[2,7,1],[3,7,2],[3,8,1]]rightRows = [[1,7,1,10],[2,7,2,20],[3,7,1,30],[3,7,2,40]]matchColumns = [1,2]return = [[2,7,1,7,1,10],[3,7,2,7,2,40],[3,8,1,-1,-1,-1]]Both key columns must match. The first left row has the same key as the first and third right rows, but the third is later than it, so it takes the first. The second left row has the same key as the second and fourth right rows. Both are early enough, and the fourth is newer, so it wins. The final left row has key (8,1). No right row has that key, so every right value column gets the fill value for a missing match.
Example 6
leftRows = [[2,7,1]]rightRows = [[2,7,1,10],[2,8,1,20],[2,7,1,30]]matchColumns = [1,2]return = [[2,7,1,7,1,30]]All three right rows share the left row's timestamp. The middle one has a different key, so it is skipped. The first and last rows both match, and the last one in the list wins. So the left row gets the value from the last right row.
Constraints
1 <= leftRows.length, rightRows.length <= 100000.- Every row in one table has the same length, and each row has between
2and10columns. - Within each table, timestamps are sorted in nondecreasing order.
- Every value is within the signed 32-bit integer range.
matchColumnscontains distinct indices. Each is at least1and less than both tables' row widths. It may be empty, and its order does not change the matching rule.- The returned table contains at most
10^6integers.
More Headlands Technologies interview questions
- Disaster RecoveryOA · Seen Nov 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
- Minimum Changes for Distinct Adjacent CharactersOA · Seen Sep 2026
- Sliding-Window Means with IEEE Special ValuesOA · Seen Sep 2026
- Exclusive Function Execution TimesOA · Seen Jan 2024
- Earliest Dependency ScheduleOA · Seen Oct 2023
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
Similar questions at other companies
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions