Quick Overview

Count distinct customers who entered a store on at least two different dates, treating repeated logs for the same customer and date as one visit date.

Count Customers Who Visited on Multiple Dates

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Each store-entry log is `[customerId, date]`, where `date` is an integer day identifier. Count how many distinct customers visited on at least two distinct dates. Multiple entries by the same customer on the same date count as one visit date. Logs may be in any order. ### Function Contract Implement `countRepeatCustomers(logs)` and return one integer. ### Constraints & Assumptions - `0 <= len(logs) <= 2,000,000`. - Customer IDs are nonempty ASCII strings. - Dates are signed 32-bit integers. - A customer is counted once regardless of how many distinct dates beyond two appear. - The input may be too large to sort comfortably, so explain the memory and streaming trade-offs of the chosen approach. ### Clarifying Questions to Ask - Do two entries on one date make a repeat customer? No. - Is the result the number of repeat visits or repeat customers? Distinct customers. - Can processing stop tracking all dates after a customer reaches two? Yes, only the first distinct date and repeat status are needed. ```hint Store the first distinct date For each customer, remember the first date. When a different date appears, mark the customer repeated and increment the answer only on that transition. ``` ### Examples ```text logs = [["a",1], ["a",1], ["b",2], ["a",3], ["b",2]] -> 1 logs = [["a",1], ["a",2], ["a",3], ["b",4], ["b",5]] -> 2 logs = [] -> 0 ``` ### Evaluation Focus - Deduplicates repeated same-day entries. - Counts each customer at most once. - Works without depending on input order. - Uses `O(number of distinct customers)` state and linear expected time. ### Extensions to Discuss 1. How would you filter logs to an inclusive time range before counting? 2. How could a partitioned data pipeline guarantee all records for one customer meet at the same reducer? 3. What approximation would you consider if even one state record per customer is too large?

Quick Answer: Count distinct customers who entered a store on at least two different dates, treating repeated logs for the same customer and date as one visit date.

Each store-entry log is `[customerId, date]`, where `customerId` is a nonempty ASCII string and `date` is an integer day identifier. Return the number of distinct customers that appear on at least two distinct dates. Multiple entries for one customer on the same date count as one visit date, logs may arrive in any order, and each qualifying customer is counted only once regardless of later dates.

Constraints

  • The input contains 0 through 2000000 log pairs.
  • Each customer ID is a nonempty ASCII string.
  • Each date is a signed 32-bit integer day identifier.
  • Same-day duplicate entries count once, and each customer is counted at most once after reaching two distinct dates.

Examples

Input: ([['a', 1], ['a', 1], ['b', 2], ['a', 3], ['b', 2]],)

Expected Output: 1

Explanation: In the first source example only customer a appears on two distinct dates.

Input: ([['a', 1], ['a', 2], ['a', 3], ['b', 4], ['b', 5]],)

Expected Output: 2

Explanation: In the second source example both customers transition to repeated status exactly once.

Hints

  1. Remember only each customer's first distinct date and whether that customer has already repeated.
  2. Increment the answer only on the transition from one known date to a different date.

Loading coding console...