Count Shoppers With Visits on Two or More Days
Arrays & HashingAuto-graded · 16 test casesLast updated
Group log entries by person and ignore repeats on the same day, then count how many people appear on at least two different calendar days.
A shop records every time a shopper passes through its entrance gate. Each record is a pair holding the shopper's ID and the calendar date of that entry.
A shopper may come through the gate several times in one day. All entries on the same date count as a single day of presence.
Write a function that returns how many different shoppers were present on at least two different dates.
Input
entries: a list of records, where each record is[shopper_id, entry_date].shopper_idis a string identifying the shopper. IDs are case-sensitive, so"Bob"and"bob"are different shoppers.entry_dateis a string in"YYYY-MM-DD"format. Two records fall on the same date exactly when their date strings are equal.
Records may appear in any order.
Output
Return an integer: the number of distinct shoppers whose records contain two or more distinct dates.
Rules and edge cases
- If
entriesis empty, return0. - Many records for the same shopper on the same date must be treated as one date.
- If every record has the same date, return
0.
Constraints
0 <= len(entries) <= 2 * 10^51 <= len(shopper_id) <= 32, alphanumeric characters onlyentry_dateis always a valid date in"YYYY-MM-DD"format
Examples
Example 1
Input: count_repeat_day_shoppers([['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
Example 2
Input: count_repeat_day_shoppers([['alice', '2023-10-05'], ['bob', '2023-10-05'], ['alice', '2023-10-06'], ['bob', '2023-10-07'], ['alice', '2023-10-05']])
Output: 2
Hints
Hint 1
Counting how many records a shopper has is not enough, because several records may share one date. Think about what you need to track per shopper instead.
Hint 2
Use a dictionary keyed by shopper ID whose values are sets of date strings. The set automatically discards repeated dates.
Hint 3
You never need more than two dates per shopper. Once a shopper has two different dates, you can stop tracking them and just count them once.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task is to find shoppers with at least two distinct dates, so two steps are needed: group the records by shopper, and remove duplicate dates within each group.
A dictionary mapping each shopper ID to a set of dates does both in a single pass. The dictionary does the grouping, and the set ignores repeated insertions of the same date. After processing all records, count the shoppers whose set has size 2 or more.
Because dates are normalized "YYYY-MM-DD" strings, string equality is the same as date equality, so no date parsing is needed.
Why it is correct
- After the loop, the set for shopper
scontains exactly the distinct dates on whichshas a record. - Sets ignore duplicates, so extra entries on the same day cannot raise the count. This is the main pitfall: counting records per shopper instead of distinct dates would wrongly include someone who came twice on one day.
- Each shopper is a single key in the dictionary, so each qualifying shopper is counted exactly once.
- An empty input leaves the dictionary empty, giving
0. If all records share one date, every set has size 1, giving0.
Complexity
- Time: O(n) expected, where n is the number of records. Each record costs one dictionary lookup and one set insertion; the strings are at most 32 and 10 characters long, so hashing is effectively constant. The final pass over shoppers is O(n) at worst.
- Space: O(n) in the worst case, for storing each distinct (shopper, date) pair.
A lower-memory variant stores only the first date seen for each shopper and flips it to a "qualified" marker the first time a different date appears, giving O(k) space for k shoppers.
Reference solution
from typing import List
def count_repeat_day_shoppers(entries: List[List[str]]) -> int:
dates_by_shopper = {}
for shopper_id, entry_date in entries:
dates = dates_by_shopper.setdefault(shopper_id, set())
dates.add(entry_date)
return sum(1 for dates in dates_by_shopper.values() if len(dates) >= 2)