Count Customers Who Visited on Multiple Dates
AmazonSoftware Engineer · Technical ScreenLast updated
Tests data manipulation, hash set/map aggregation, and date deduplication to identify users with repeat visits across distinct calendar days.
Problem Statement
An Amazon Go store maintains an entry log recording each time a customer walks through the turnstile. Each record consists of a customer_id and a timestamp or date string.
A customer may enter the store multiple times on the same calendar day (e.g., buying a morning coffee and returning for lunch). These same-day visits only count as a single visit date.
Write a function that returns the total number of distinct customers who have visited the store on at least two different calendar dates.
Function Signature
def count_multi_date_visitors(logs: list[list[str]]) -> int:
pass
Input and Output Format
- Input: A list of pairs
logs, where each element is[customer_id, visit_date].customer_id: A string representing the unique identifier for a customer.visit_date: A string representing the date in"YYYY-MM-DD"format.
- Output: An integer representing the count of unique customers who visited on at least two distinct dates.
Constraints
0 <= len(logs) <= 2 * 10^51 <= len(customer_id) <= 32(alphanumeric characters)visit_dateis a valid date string in"YYYY-MM-DD"format.
Examples
Example 1
Input: logs = [
["cust_1", "2023-11-01"],
["cust_2", "2023-11-01"],
["cust_1", "2023-11-01"],
["cust_1", "2023-11-02"],
["cust_3", "2023-11-03"]
]
Output: 1
Explanation:
cust_1visited on"2023-11-01"and"2023-11-02"(2 distinct dates).cust_2visited only on"2023-11-01"(1 distinct date).cust_3visited only on"2023-11-03"(1 distinct date). Onlycust_1visited on at least two distinct dates.
Example 2
Input: logs = [
["alice", "2023-10-05"],
["bob", "2023-10-05"],
["alice", "2023-10-06"],
["bob", "2023-10-07"],
["alice", "2023-10-05"]
]
Output: 2
Explanation:
Both alice ("2023-10-05", "2023-10-06") and bob ("2023-10-05", "2023-10-07") visited on two distinct dates.
Edge Cases
logsis empty: return0.- A customer visits multiple times on the same date: multiple entries for the same date must not inflate the distinct date count.
- All visits belong to a single date across all customers: return
0.
Hints
Hint 1
You need to group dates by customer and deduplicate dates for each customer.
Hint 2
A hash map from customer ID to a set of visit dates allows O(1) deduplication per entry.
Hint 3
To save memory, you do not need to store every date for a customer—tracking a single previous date and a boolean flag once a second date is seen is sufficient.
Follow-up questions
- How would you solve this if the input data was too large to fit in memory on a single machine (e.g., using MapReduce or SQL)?
- How would your approach change if you needed to find customers who visited at least two distinct dates within a rolling 7-day window?
- If raw timestamps with different timezones were provided instead of normalized `YYYY-MM-DD` strings, how would you handle determining the store's local date?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.