Crossed User Pairs on One Date
Updated
Problem statement
A service writes one log entry each time a user does something. Entry i has a date dates[i], a time times[i] (whole seconds after midnight on that date) and the user's id userIds[i]. New entries are only ever added to the end of the log, so the dates never go down, and within one date the times never go down. Several entries can share a time, and one user can appear many times.
Two entries on the same date form a crossed pair when one of them has an earlier time but a larger user id than the other. That is, entries i and j form a crossed pair when times[i] < times[j] and userIds[i] > userIds[j]. Two entries with the same time, or with the same user id, never form a crossed pair.
Return the number of crossed pairs among the entries whose date is date. Each pair of entries counts once. If no entry has that date, return 0.
Function
countCrossedPairs(dates: String[], times: int[], userIds: int[], date: String) → intExamples
Example 1
dates = ["2024-06-02","2024-06-03","2024-06-03","2024-06-03","2024-06-03","2024-06-04"]times = [86000,10,20,20,35,5]userIds = [5,9,4,7,2,1]date = "2024-06-03"return = 5Only the four entries dated 2024-06-03 count. User 9 at time 10 crosses users 4 and 7 at time 20 and user 2 at time 35, which is 3 pairs. Users 4 and 7 at time 20 each cross user 2 at time 35, which is 2 more. Users 4 and 7 share time 20, so they do not form a pair. The answer is 5.
Example 2
dates = ["2024-06-01","2024-06-01"]times = [100,200]userIds = [8,3]date = "2024-06-05"return = 0No entry has the date 2024-06-05, so the answer is 0. The crossed pair on 2024-06-01 does not count.
Example 3
dates = ["2024-01-15","2024-01-15","2024-01-15","2024-01-15","2024-01-15"]times = [0,0,60,60,120]userIds = [7,3,7,5,3]date = "2024-01-15"return = 4User 7 at time 0 crosses user 5 at time 60 and user 3 at time 120 (2 pairs), but not user 7 at time 60, because the ids are equal. User 3 at time 0 has no later entry with a smaller id. User 7 and user 5 at time 60 each cross user 3 at time 120 (2 pairs). The two entries at time 0 share a time, so they do not pair. The answer is 4.
Constraints
1 <= dates.length <= 3000, andtimesanduserIdshave the same length asdates- Every
dates[i], anddate, is a real calendar date written asYYYY-MM-DD. 0 <= times[i] <= 863991 <= userIds[i] <= 10^9- The entries are in the order they were written: the dates never go down, and among entries with the same date, the times never go down.
More Rippling Onsite Interview questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delivery Cost TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Maximum Currency Conversion Along a Simple PathPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent WordsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Future-Effective Employee SalaryONSITE INTERVIEW · Seen Aug 2026
- Filter and Sort Scheduled TasksONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions
- Mutual-Friend RecommendationsMeta · ONSITE INTERVIEWMeta interview questions
- Top-K URLs Overall and in the Last 24 HoursOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Read a File from Distributed ChunksHudson River Trading · ONSITE INTERVIEWHudson River Trading interview questions
- Top K Frequent ElementsInfosys · ONSITE INTERVIEWInfosys interview questions
- Nickname-Constrained GroupingZoox · ONSITE INTERVIEW