FastPrepPins Violation Log Queries
Problem · Array

Pins Violation Log Queries

Learn this problem
MediumPinterest logoPinterestFULLTIMEPHONE SCREEN

Problem statement

You are given timestamp-sorted violation records. Each row is [postId, policy, timestamp], where timestamp is a decimal integer string. Duplicate rows describe the same violation and count once.

Answer each query with one canonical string:

  • ["POSTS_BY_POLICY", policy]: distinct post IDs for that policy, sorted lexicographically and joined by commas.
  • ["POLICIES_BY_POST", postId]: distinct policies for that post, sorted lexicographically and joined by commas.
  • ["POSTS_AT_TIME", timestamp]: distinct post IDs at that exact time, sorted lexicographically and joined by commas.
  • ["POSTS_IN_RANGE", start, end]: distinct post IDs with a violation in the inclusive time range, sorted lexicographically and joined by commas.
  • ["COUNTS_BY_POLICY", start, end]: distinct-violation counts in the inclusive range, formatted as sorted policy=count pairs joined by commas.

Return the answer strings in query order. Return the empty string when a set or map result is empty.

Function

answerViolationQueries(records: String[][], queries: String[][]) → String[]

Examples

Example 1

records = [["p2","spam","10"],["p1","spam","10"],["p1","hate","12"],["p2","spam","15"]]queries = [["POSTS_BY_POLICY","spam"],["POLICIES_BY_POST","p1"],["POSTS_AT_TIME","10"],["POSTS_IN_RANGE","11","15"],["COUNTS_BY_POLICY","10","12"]]return = ["p1,p2","hate,spam","p1,p2","p1,p2","hate=1,spam=2"]

The first four answers list unique sorted identifiers. The final answer counts the three distinct violation rows at times 10 through 12.

Constraints

  • 1 <= records.length, queries.length <= 10^5.
  • Post IDs and policies are nonempty strings containing letters, digits, hyphens, or underscores.
  • Timestamps are decimal integers in [0, 10^18].
  • Records are sorted by nondecreasing timestamp.
  • Every query has one of the five documented forms.

More Pinterest problems

drafts saved locally
public String[] answerViolationQueries(String[][] records, String[][] queries) {
    // write your code here
}
records[["p2","spam","10"],["p1","spam","10"],["p1","hate","12"],["p2","spam","15"]]
queries[["POSTS_BY_POLICY","spam"],["POLICIES_BY_POST","p1"],["POSTS_AT_TIME","10"],["POSTS_IN_RANGE","11","15"],["COUNTS_BY_POLICY","10","12"]]
expected["p1,p2", "hate,spam", "p1,p2", "p1,p2", "hate=1,spam=2"]
checking account