TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Top-K Recommendations

Recommender Systems
Easy

The final step in most recommender systems is selecting the top-K items to present to a user. Given predicted scores for all items and a set of items the user has already rated, the system must return the K highest-scoring items that the user has not yet seen.

Given a list of predicted scores (one per item), a collection of already-rated item indices, and a count K, return the indices of the top-K unrated items sorted by descending score.

Algorithm

  1. Filter out items that appear in the rated set
  2. Sort the remaining items by their predicted score in descending order
  3. Return the indices of the top K items

Return at most k unrated indices ordered by descending score; break ties by smaller index.

Loading visualization...

Examples

Input: scores = [3.5, 1.2, 4.8, 2.1, 5.0], rated_indices = [0, 2], k = 2

Output: [4, 3]

Explanation: After removing items 0 and 2, items 4 and 3 have the two highest scores.

Input: scores = [1.0, 3.0, 2.0], rated_indices = [], k = 2

Output: [1, 2]

Hint 1

Build score-index pairs only for indices absent from rated_indices.

Hint 2

Sort by negative score and then index before taking the first k pairs.

Requirements

  • Exclude all items whose indices appear in rated_indices
  • Sort remaining items by predicted score in descending order
  • Return the indices of the top K items as a list
  • Break equal scores by choosing the smaller item index first
  • If fewer than K items are unrated, return all unrated item indices

Constraints

  • scores is a non-empty list of floats
  • rated_indices is a list or set of valid indices
  • 1 <= k <= len(scores)
  • Return a list of integers (item indices)
  • Time limit: 300 ms
Try Similar Problems
Hit Rate At KPrecision Recall At KNdcgPopularity RankingItem Cf Predict

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Top-K Recommendations

Recommender Systems
Easy

The final step in most recommender systems is selecting the top-K items to present to a user. Given predicted scores for all items and a set of items the user has already rated, the system must return the K highest-scoring items that the user has not yet seen.

Given a list of predicted scores (one per item), a collection of already-rated item indices, and a count K, return the indices of the top-K unrated items sorted by descending score.

Algorithm

  1. Filter out items that appear in the rated set
  2. Sort the remaining items by their predicted score in descending order
  3. Return the indices of the top K items

Return at most k unrated indices ordered by descending score; break ties by smaller index.

Loading visualization...

Examples

Input: scores = [3.5, 1.2, 4.8, 2.1, 5.0], rated_indices = [0, 2], k = 2

Output: [4, 3]

Explanation: After removing items 0 and 2, items 4 and 3 have the two highest scores.

Input: scores = [1.0, 3.0, 2.0], rated_indices = [], k = 2

Output: [1, 2]

Hint 1

Build score-index pairs only for indices absent from rated_indices.

Hint 2

Sort by negative score and then index before taking the first k pairs.

Requirements

  • Exclude all items whose indices appear in rated_indices
  • Sort remaining items by predicted score in descending order
  • Return the indices of the top K items as a list
  • Break equal scores by choosing the smaller item index first
  • If fewer than K items are unrated, return all unrated item indices

Constraints

  • scores is a non-empty list of floats
  • rated_indices is a list or set of valid indices
  • 1 <= k <= len(scores)
  • Return a list of integers (item indices)
  • Time limit: 300 ms
Try Similar Problems
Hit Rate At KPrecision Recall At KNdcgPopularity RankingItem Cf Predict

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.