Code Room
CodingHardcod-g1073
Subject Machine learningLevel Senior~35 minCommon in ML systems interviewsIndustries Software development, Technology

Question

Implement a single user-based collaborative-filtering prediction. You are given ratings, a dict mapping user id (string) to a dict of {item id (string): rating (number)}; a target user u; and an item i. Predict u's rating for i as the cosine-weighted average of the ratings other users gave to i, weighted by their cosine similarity to u (cosine over the items both users have rated; 0 if they share no items). Only include neighbors who have rated i and whose similarity to u is strictly positive. If the sum of used similarities is 0 (no positive-similarity neighbor rated i), return 0.0. Return the predicted rating rounded to 6 decimals.

Implement
predict_rating(ratings: dict, u: str, i: str) → float
Examples
in[{"u":{"a":5,"b":3},"v":{"a":4,"b":2,"c":5},"w":{"a":1,"c":1}},"u","c"]out3.091836
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.