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: Complete ProcessPositivesmart, brilliantNegativenot, badReportsStudent 1: smart workStudent 2: not goodStudent 1: brilliantScore CalculationStudent 1: 3 + 3 = 6Student 2: -1 = -1Top K Ranking1st: Student 1 (6)2nd: Student 2 (-1)Final Result (k=2)[1, 2]Sorted by score descending, then ID ascending
Understanding the Visualization
1
Input Processing
Parse positive/negative feedback and student reports
2
Score Calculation
Calculate each student's total score from all their reports
3
Ranking & Selection
Sort by score (desc) then ID (asc), return top k
Key Takeaway
🎯 Key Insight: Use hash sets for fast word lookup, then custom sort by score and ID for efficient top-k selection
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