FastPrepLoyal Customers by Two-Day Product Variety
Problems/Loyal Customers by Two-Day Product Variety

Loyal Customers by Two-Day Product Variety

Amazon logoAmazon● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the two-day loyalty rule and set-based solution direction, but omits the callable interface and output order. The judged core task matches the report at about 97%.

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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
public String[] loyalCustomers(String[][] dayOneLogs, String[][] dayTwoLogs) {
  // Write your code here
}
Checking account…