← 返回 pinterest 的题目列表Policy Violation Records Aggregation
类型:qbank
Given violation records `(pinId, policy, date)`, implement three queries: unique pins that violated a given policy, unique pins that violated any policy inside a date range, and unique pins per policy inside a date range. The interviewer leaves return shapes and duplicate handling for the candidate to clarify, and duplicate `pinId` records are explicitly part of the problem.
Requirements
Input records have the shape:
(pinId: int, policy: string, date: string)
Implement query support for:
Given a policy, return the number of unique pins that violated that policy.
Given a date range [startDate, endDate], return the number of unique pins that violated any policy inside that range.
Given a date range [startDate, endDate], return the number of unique pins per policy inside that range.
Clarify the return type before coding. The problem statement leaves room for returning a scalar count for the first two operations and a policy -> count map for the third.
Notes
Duplicate pinId records are an explicit edge case. Count unique pins, not violation rows.
A straightforward implementation stores policy -> Set<pinId> for the first query and scans records for range queries, accumulating either one global Set<pinId> or a Map<policy, Set<pinId>>. That is acceptable as a base solution if the input size is moderate.
The follow-up pressure is indexing: for many date-range queries, pre-sort records by date and binary-search the active slice, or maintain date-bucketed maps that merge into sets over the requested range. The trade-off is memory versus repeated scan cost.
Be explicit about date comparison semantics. If dates are ISO yyyy-mm-dd, lexicographic comparison works; otherwise parse to a numeric day or timestamp once during ingestion.
The interviewer may be quiet and leave ambiguity unresolved. State assumptions out loud: inclusive range bounds, whether the same pin violating two policies counts once globally, and what to return when no violations match.
Preparation
Implement the scan-based version first: parse records, dedupe with sets, and write the three query methods cleanly.
Drill the indexed variant separately: sort by date, use two binary searches to find the range, then aggregate unique pins without double-counting.
Practice explaining memory trade-offs between policy -> Set<pinId>, date -> records, and date -> policy -> Set<pinId> indexes.