Problem · Array
Loyal Customers by Two-Day Product Variety
Learn this problemProblem statement
You are given website interaction logs from two consecutive days. Every row in dayOneLogs and dayTwoLogs contains [customerId, productId].
A customer is loyal when both conditions hold:
- The customer appears at least once on each day.
- Across both days combined, the customer interacts with at least two distinct products.
Repeated interactions with the same product count once. Return all loyal customer IDs in lexicographically increasing order.
Function
loyalCustomers(dayOneLogs: String[][], dayTwoLogs: String[][]) → String[]Examples
Example 1
dayOneLogs = [["u1","p1"],["u1","p1"],["u2","p2"],["u3","p9"]]dayTwoLogs = [["u1","p2"],["u2","p2"],["u2","p3"],["u4","p1"]]return = ["u1","u2"]Customers u1 and u2 appear on both days. Their combined distinct-product sets are respectively {p1, p2} and {p2, p3}.
Example 2
dayOneLogs = [["amy","book"],["bob","pen"]]dayTwoLogs = [["amy","book"],["bob","paper"]]return = ["bob"]Both customers appear on both days, but amy interacts only with book. Customer bob interacts with two distinct products.
Example 3
dayOneLogs = [["a","x"],["a","y"]]dayTwoLogs = [["b","x"],["b","y"]]return = []No customer appears on both days, so the result is empty even though each customer has two distinct products.
Constraints
0 <= dayOneLogs.length, dayTwoLogs.length <= 2 * 10^5.- Every log row contains exactly two non-empty strings: a customer ID followed by a product ID.
- Every ID contains only English letters, digits, hyphens, and underscores.
- The total number of log rows across both days is at most
2 * 10^5.