Your question is Top-K Frequent Item Pairs. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Quinstreet comparison surfaces record user sessions as sequences of interacted item IDs. Given these sessions, find the k most frequent unordered item pairs that appeared together in at least one session.
A pair counts at most once per session, even if a user interacted with the same item repeatedly. Return pairs ordered by decreasing frequency. When frequencies tie, order pairs lexicographically by their two item IDs.
Implement top_k_item_pairs(sessions, k), where sessions is a list of lists of non-negative integer item IDs and k is a positive integer. Return a list of pairs, represented as two-element lists [smaller_id, larger_id]. Return fewer than k pairs if fewer distinct pairs exist.
def top_k_item_pairs(sessions, k):