Solve set equality and ad log top‑K
PinterestSoftware Engineer · OnsiteLast 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]→trueA = [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)eventsis sorted bytimestamp_msascending.
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
sourcecan be used at most once. - Return
trueif possible, elsefalse.
Example
source = "aab",target = "aba"→truesource = "ab",target = "abb"→false
Constraints
- Strings consist of ASCII letters (you may assume lowercase
a-zif you want to simplify). - Length up to
2e5.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.