KatInterview

Nearest Profiles by Masked Mean Squared Error

Heaps, Sorting & SearchAuto-graded · 14 test cases
HardAuto-gradedHeaps, Sorting & Searchsortingsimilaritymissing-data

Last updated

Tests pairwise distance computation with a missing-data-aware MSE metric, tie-breaking, and top-K ranking across user profiles.

Problem

You have a collection of user profiles. Each profile has a unique string identifier ("user_id") and some number of numeric feature fields. Feature values may be absent (represented as None or simply missing from the dict).

Your task is to find the 5 closest profiles to each user, where closeness is measured by a missing-data-aware Mean Squared Error (MSE).


Distance Definition

For two profiles P and Q, let the shared features be every feature key where both profiles have a non-None value.

  • If the number of shared features is less than 2, the pair is considered incomparable and must be excluded from all neighbor lists.
  • Otherwise, the distance is:
distance(P, Q) = sum((P[f] - Q[f])^2 for f in shared features) / |shared features|

The divisor is the count of shared features, not the total number of features in the dataset.


What to Return

Return a dictionary mapping every user_id to a list of up to 5 other user_ids, ordered by:

  1. Distance ascending
  2. user_id lexicographically ascending as a tie-breaker

A profile is never its own neighbor. If a profile has no comparable neighbors, map it to an empty list. Lists may contain fewer than 5 entries when fewer comparable profiles exist.


Input Format

A list of dicts. Each dict contains:

  • "user_id": a non-empty string, unique across all profiles
  • Zero or more feature keys mapped to float / int values, or None

Feature keys are derived from the keys present in the input (excluding "user_id"). A key absent from a dict is treated the same as None.


Constraints

  • 0 ≤ number of profiles ≤ 1,000
  • 0 ≤ number of feature columns ≤ 50
  • user_id values are non-empty strings
  • Numeric feature values, when present, are finite floats or ints
  • NaN will not appear; absent or None values represent missingness
  • An empty input list must return an empty dict

Examples

Example 1

Input: find_closest_profiles([{'user_id': 'A', 'f1': 1, 'f2': 2}, {'user_id': 'B', 'f1': 1.2, 'f2': 2.1}, {'user_id': 'C', 'f1': 5, 'f2': 5}])

Output: {'A': ['B', 'C'], 'B': ['A', 'C'], 'C': ['B', 'A']}

Hints

Hint 1

For each ordered pair (i, j), iterate over the feature keys and accumulate squared differences only where both profiles have a non-None value. Track the count of those shared features separately from the total.

Hint 2

Before computing any distances, collect the full list of feature keys once (everything except 'user_id' in the first row). Use dict.get(key) so that missing keys are treated as None automatically.

Hint 3

After collecting each user's candidate list of (distance, neighbor_id) tuples, sort by (distance, neighbor_id) to get ascending distance with lexicographic tie-breaking, then slice [:5].