Bought together: count the right baskets
transcript
A product page shows related products. The shop keeps a list of many past baskets. Which products most often belong together?
Look only at the baskets that hold the target. Skip every other basket in the list. Never count the target itself as a match.
Four baskets, and three hold product one. The third basket has no product one. Seven, three and nine each appear twice.
Rank by the higher count first. On equal counts, the smaller id. Then keep the first k.
Count only baskets with the target. Count all baskets, and product three jumps ahead.
The count must go down. The id must go up. Put both in one comparison.
In the interview, say which baskets count. Say what a tie means. And say what the target is.
Use one pass over the baskets. Keep a map of counts. Use a heap for the top k.
Target baskets only. Count down, id up. Full breakdown in the lecture.