Given this list, design a class to help us efficiently answer the following questions:
- Given a policy, find out the number of unique pins that violate that policy.
- Given a pin id, find out which policies it violates.
- Given a date, find out the number of violating pins.
- Given a date range, find out the number of violating pins.
- Given a date range, find out how many times each policy was violated.
Example answers using the list given above:
- For input policy
"self_harm", answer is2. - For input pin id
1, answer is["spam", "self_harm"]. - For input date
"2022-06-02", answer is2. - For date range
("2022-06-01", "2022-06-05"), answer is3. - For date range
("2022-06-01", "2022-06-05"), answer is{"spam": 2, "self_harm": 1}.
This problem is about designing a data structure for efficient violation analytics over pin records. Each record contains a pin id, a policy, and a date, and the class must support queries by policy, by pin id, by a single date, by a date range, and by policy counts within a date range. A good solution usually builds multiple hash-based indexes for fast lookups and deduplication, and may use ordered dates or prefix aggregation to answer range queries efficiently. The key challenges are choosing the right internal representations and handling unique counts versus total violation counts correctly.