Reward Top K Students - Problem

You are given two string arrays positive_feedback and negative_feedback, containing the words denoting positive and negative feedback, respectively. Note that no word is both positive and negative.

Initially every student has 0 points. Each positive word in a feedback report increases the points of a student by 3, whereas each negative word decreases the points by 1.

You are given n feedback reports, represented by a 0-indexed string array report and a 0-indexed integer array student_id, where student_id[i] represents the ID of the student who has received the feedback report report[i]. The ID of each student is unique.

Given an integer k, return the top k students after ranking them in non-increasing order by their points. In case more than one student has the same points, the one with the lower ID ranks higher.

Input & Output

Example 1 — Basic Case
$ Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
Output: [1,2]
💡 Note: Student 1: 'studious' (+3) = 3 points. Student 2: 'smart' (+3) = 3 points. Both have same score, so lower ID (1) ranks higher.
Example 2 — With Negative Feedback
$ Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
Output: [2,1]
💡 Note: Student 1: 'not' (-1) + 'studious' (+3) = 2 points. Student 2: 'smart' (+3) = 3 points. Student 2 ranks higher with better score.
Example 3 — Multiple Reports per Student
$ Input: positive_feedback = ["smart","brilliant"], negative_feedback = ["not"], report = ["smart","brilliant","not smart"], student_id = [1,1,2], k = 1
Output: [1]
💡 Note: Student 1: 'smart' (+3) + 'brilliant' (+3) = 6 points. Student 2: 'not' (-1) + 'smart' (+3) = 2 points. Student 1 wins.

Constraints

  • 1 ≤ positive_feedback.length, negative_feedback.length ≤ 104
  • 1 ≤ positive_feedback[i].length, negative_feedback[i].length ≤ 100
  • 1 ≤ report.length ≤ 104
  • 1 ≤ report[i].length ≤ 100
  • 1 ≤ student_id.length ≤ 104
  • 1 ≤ k ≤ student_id.length

Visualization

Tap to expand
Reward Top K Students Hash Set Optimization Approach INPUT positive_feedback: smart brilliant studious +3 negative_feedback: not -1 report[]: [0]: "this student is studious" [1]: "the student is smart" student_id[]: 1 2 k = 2 (return top 2 students) ID 1 gets report[0] ID 2 gets report[1] ALGORITHM STEPS 1 Build HashSets positive_set, negative_set O(1) word lookup 2 Process Each Report Split words, check sets +3 positive, -1 negative 3 Calculate Scores Map student_id to score ID Words Score 1 studious +3 = 3 2 smart +3 = 3 4 Sort and Return Top K By score DESC, then ID ASC Same score: lower ID first FINAL RESULT Sorted Students: Rank #1: Student ID 1 Score: 3 points 1st Rank #2: Student ID 2 Score: 3 points 2nd Tie-breaker Applied! Same score: ID 1 < ID 2 Output: [1, 2] OK - Top 2 students returned Sorted by score, then by ID Key Insight: Using HashSets for positive/negative words enables O(1) lookup per word instead of O(n) array search. Time Complexity: O(n*m + n*log(n)) where n = reports, m = avg words per report. Space: O(p + q) for sets. TutorialsPoint - Reward Top K Students | Hash Set Optimization
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
12.4K Views
Medium Frequency
~25 min Avg. Time
350 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen