Design an In-Memory Violation Log Analyzer with Policy and Time-Range Queries

Quick Overview

Design an in-memory store for policy violation events that supports appends, lookups by policy, inclusive time-range queries and per-policy counts, with results ordered by timestamp and then ID. It tests index design, binary search boundaries and accurate complexity analysis.

Design an In-Memory Violation Log Analyzer with Policy and Time-Range Queries

Company: Pinterest

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory violation log analyzer that stores violation events and supports efficient queries. Each violation event consists of a unique `violation_id`, a `policy` name (string), and a `timestamp` (integer). The system must support appending new violations and querying them either by policy name or by time range. All query results should be returned sorted by `timestamp` in ascending order, with ties broken by `violation_id` in ascending order. Additionally, the system should support counting the number of violations recorded for a specific policy. Implement the following methods: - `addViolation(violation_id: int, policy: str, timestamp: int)` appends a violation event with the given ID, policy name and timestamp to the log. - `getByPolicy(policy: str) -> List[int]` returns all violation IDs matching the given policy, sorted by timestamp (ascending), then by ID (ascending). - `getByTimeRange(startTime: int, endTime: int) -> List[int]` returns all violation IDs whose timestamp is in the inclusive range `[startTime, endTime]`, sorted by timestamp, then by ID. - `countByPolicy(policy: str) -> int` returns the total number of violations recorded for the given policy. ```hint One index per query Each query filters on a different field. Look for one structure per query shape, so that no query has to scan every stored violation. ``` ```hint Inclusive bounds With violations kept in (timestamp, id) order, a time range is a contiguous slice. Check that your two search keys include every event at `startTime` and every event at `endTime`. ``` ### Examples Example 1: queries on an empty log, then one insert. ```python analyzer = ViolationLogAnalyzer() analyzer.getByPolicy("no-helmet") # [] no violations yet analyzer.getByTimeRange(1, 1000) # [] no violations yet analyzer.countByPolicy("no-helmet") # 0 analyzer.addViolation(10, "no-helmet", 500) # log: id=10, policy=no-helmet, ts=500 analyzer.getByPolicy("no-helmet") # [10] analyzer.getByTimeRange(1, 1000) # [10] ts=500 is within [1, 1000] ``` Example 2: out-of-order inserts and a timestamp tie. ```python analyzer = ViolationLogAnalyzer() analyzer.addViolation(7, "spam", 300) analyzer.addViolation(3, "hate-speech", 100) analyzer.addViolation(5, "spam", 300) analyzer.addViolation(2, "spam", 200) analyzer.getByPolicy("spam") # [2, 5, 7] ts 200, then the tie at ts 300 in ID order analyzer.getByTimeRange(200, 300) # [2, 5, 7] both ends of the range are included analyzer.getByTimeRange(100, 250) # [3, 2] ordered by timestamp first, then ID analyzer.countByPolicy("spam") # 3 analyzer.countByPolicy("violence") # 0 ``` ### Constraints & Assumptions - `1 <= violation_id <= 10^6` - `1 <= len(policy) <= 50` - `policy` consists of lowercase English letters, digits and hyphens - `1 <= timestamp <= 10^9` - All `violation_id` values are unique - At most `10^4` calls in total across all methods - For `getByTimeRange`, `startTime <= endTime` ### Clarifying Questions to Ask - Do violations arrive in timestamp order, or can an event be added with a timestamp older than ones already stored? - What is the expected mix of writes and reads, and which query is the most frequent? - Can violations ever be removed or edited after they are added? - For a policy that has never been seen, should `getByPolicy` return an empty list and `countByPolicy` return 0? ### What a Strong Answer Covers - A per-policy index and a time-ordered index, and which query each one serves. - A `(timestamp, violation_id)` ordering and binary search boundaries that keep both ends of the time range inclusive. - An accurate cost for every method, including what a sorted insert into an array really costs. - Behavior for unknown policies, empty ranges, timestamp ties and out-of-order inserts. - When a different structure (lazy sorting, a balanced tree, time buckets) would fit the workload better. ### Follow-up Questions - How would the design change if writes far outnumber reads, or the log grows past what fits in memory? - If each event also carried a post ID, how would you return the policies a given post violated? - How would you count one policy's violations inside a time range without listing them? - How would you make the analyzer safe with concurrent writers and readers?

Overview: Design an in-memory store for policy violation events that supports appends, lookups by policy, inclusive time-range queries and per-policy counts, with results ordered by timestamp and then ID. It tests index design, binary search boundaries and accurate complexity analysis.

Pinterest logo
Pinterest
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory violation log analyzer that stores violation events and supports efficient queries. Each violation event consists of a unique violation_id, a policy name (string), and a timestamp (integer).

The system must support appending new violations and querying them either by policy name or by time range. All query results should be returned sorted by timestamp in ascending order, with ties broken by violation_id in ascending order.

Additionally, the system should support counting the number of violations recorded for a specific policy.

Implement the following methods:

  • addViolation(violation_id: int, policy: str, timestamp: int) appends a violation event with the given ID, policy name and timestamp to the log.
  • getByPolicy(policy: str) -> List[int] returns all violation IDs matching the given policy, sorted by timestamp (ascending), then by ID (ascending).
  • getByTimeRange(startTime: int, endTime: int) -> List[int] returns all violation IDs whose timestamp is in the inclusive range [startTime, endTime] , sorted by timestamp, then by ID.
  • countByPolicy(policy: str) -> int returns the total number of violations recorded for the given policy.

Examples

Example 1: queries on an empty log, then one insert.

analyzer = ViolationLogAnalyzer()

analyzer.getByPolicy("no-helmet")            # []   no violations yet
analyzer.getByTimeRange(1, 1000)             # []   no violations yet
analyzer.countByPolicy("no-helmet")          # 0
analyzer.addViolation(10, "no-helmet", 500)  # log: id=10, policy=no-helmet, ts=500
analyzer.getByPolicy("no-helmet")            # [10]
analyzer.getByTimeRange(1, 1000)             # [10]  ts=500 is within [1, 1000]

Example 2: out-of-order inserts and a timestamp tie.

analyzer = ViolationLogAnalyzer()

analyzer.addViolation(7, "spam", 300)
analyzer.addViolation(3, "hate-speech", 100)
analyzer.addViolation(5, "spam", 300)
analyzer.addViolation(2, "spam", 200)

analyzer.getByPolicy("spam")         # [2, 5, 7]  ts 200, then the tie at ts 300 in ID order
analyzer.getByTimeRange(200, 300)    # [2, 5, 7]  both ends of the range are included
analyzer.getByTimeRange(100, 250)    # [3, 2]     ordered by timestamp first, then ID
analyzer.countByPolicy("spam")       # 3
analyzer.countByPolicy("violence")   # 0

Constraints & Assumptions

  • 1 <= violation_id <= 10^6
  • 1 <= len(policy) <= 50
  • policy consists of lowercase English letters, digits and hyphens
  • 1 <= timestamp <= 10^9
  • All violation_id values are unique
  • At most 10^4 calls in total across all methods
  • For getByTimeRange , startTime <= endTime

Clarifying Questions to Ask Guidance

  • Do violations arrive in timestamp order, or can an event be added with a timestamp older than ones already stored?
  • What is the expected mix of writes and reads, and which query is the most frequent?
  • Can violations ever be removed or edited after they are added?
  • For a policy that has never been seen, should getByPolicy return an empty list and countByPolicy return 0?

What a Strong Answer Covers Guidance

  • A per-policy index and a time-ordered index, and which query each one serves.
  • A (timestamp, violation_id) ordering and binary search boundaries that keep both ends of the time range inclusive.
  • An accurate cost for every method, including what a sorted insert into an array really costs.
  • Behavior for unknown policies, empty ranges, timestamp ties and out-of-order inserts.
  • When a different structure (lazy sorting, a balanced tree, time buckets) would fit the workload better.

Follow-up Questions Guidance

  • How would the design change if writes far outnumber reads, or the log grows past what fits in memory?
  • If each event also carried a post ID, how would you return the policies a given post violated?
  • How would you count one policy's violations inside a time range without listing them?
  • How would you make the analyzer safe with concurrent writers and readers?