Requirements
- Input: user browsing sessions, a target product ID, and k.
- Count how often other products occur in sessions containing the target.
- Return the k most frequent other products as recommendations.
- Follow-up: discuss refactoring into production-quality Java, including streams and maps.
Notes
Tie handling and duplicate occurrences within a session are unspecified.
Filter to sessions containing the target, exclude the target itself, and count the remaining products in a hash map. Decide whether a product contributes once per session or once per occurrence before counting. Sort the distinct candidates by descending count and the agreed tie rule, then take up to k. For T input product occurrences and U distinct candidate products, this straightforward approach takes O(T + U log U) time and O(U) auxiliary space with expected constant-time hashing. A bounded heap can replace the full sort when k is small; define behavior for nonpositive k before implementation.
Preparation
- In 20 minutes, implement session filtering and frequency ranking. Test target absence, repeated products, tied counts, and k larger than the number of candidates under an explicitly chosen duplicate/tie convention.
- In 15 minutes, refactor the counting and selection stages into separate Java methods, then verify that the refactor preserves the chosen outputs and ordering.

