Katkat

Solve set equality and ad log top‑K

PinterestSoftware Engineer · Onsite

Last updated

This multi-part question evaluates set and multiset operations, frequency counting, top-K computation over time windows, sliding-window and streaming data structures, and character-multiset matching for string construction within the coding & algorithms domain.

Problem Set (Coding)

1) Check whether two sets are equal

You are given two integer arrays A and B that represent sets, except they may contain duplicates and are not sorted.

Return true if they represent the same mathematical set (same unique elements), otherwise return false.

Example

  • A = [1,2,2,3], B = [3,1,2] → true
  • A = [1,2], B = [1,2,4] → false

Constraints

  • 0 <= len(A), len(B) <= 2e5
  • Values fit in 32-bit signed integer.

2) Top-K ads from impression logs + sliding window ingestion

You are given an impression log as a list of events:

  • events[i] = (timestamp_ms, ad_id)
  • events is sorted by timestamp_ms ascending.

Part 1 (batch): Given events, a window size W_ms, and an end time T_end, return the top K ad IDs by number of impressions whose timestamps fall in [T_end - W_ms, T_end].

Break ties by smaller ad_id (or specify a deterministic rule).

Part 2 (streaming): Implement a data structure with two operations:

  • ingestImpression(timestamp_ms, ad_id)
  • getTopK(T_end, W_ms, K)

such that it efficiently supports a sliding window query over recent impressions.

Assume ingestImpression timestamps are non-decreasing.


3) Can a target string be formed from a source string?

Given two strings source and target, determine if you can form target using characters from source.

  • Each character in source can be used at most once.
  • Return true if possible, else false.

Example

  • source = "aab", target = "aba" → true
  • source = "ab", target = "abb" → false

Constraints

  • Strings consist of ASCII letters (you may assume lowercase a-z if you want to simplify).
  • Length up to 2e5.