Pin 面试真题解析:设计一个按 Pin 和日期统计政策违规的类

17次阅读
没有评论

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 is 2.
  • For input pin id 1, answer is ["spam", "self_harm"].
  • For input date "2022-06-02", answer is 2.
  • For date range ("2022-06-01", "2022-06-05"), answer is 3.
  • For date range ("2022-06-01", "2022-06-05"), answer is {"spam": 2, "self_harm": 1}.

这道题考察的是如何为违规记录设计一个高效查询系统。输入是一组记录,每条记录包含 pin id、policy 和日期,需要支持按 policy 查唯一违规 pin 数、按 pin id 查命中的 policy、按某一天或日期区间统计违规 pin 数,以及按日期区间汇总每个 policy 的违规次数。核心思路通常是用多个哈希映射分别建立反向索引:pin 到 policy 集合、policy 到 pin 集合、日期到记录集合,并在日期范围查询时结合前缀统计或有序结构来减少重复扫描。题目重点在于数据建模、去重、以及对区间查询的高效支持。

正文完
 0