Reward Top K Students - Problem

Imagine you're building a student evaluation system for a coding bootcamp! ๐ŸŽ“

You have feedback reports from instructors about students, and you need to automatically rank students based on their performance to reward the top performers.

Here's how the scoring works:

  • Each positive word in a feedback report gives +3 points
  • Each negative word in a feedback report gives -1 point
  • Students start with 0 points

Your task: Given feedback reports and student IDs, return the top K students ranked by their total points. If students have the same points, the one with the lower student ID ranks higher.

Input: Arrays of positive/negative words, feedback reports, student IDs, and K

Output: Array of top K student IDs in descending order by points

Input & Output

example_1.py โ€” 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.py โ€” Mixed 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 has higher score.
example_3.py โ€” Neutral Words
$ Input: positive_feedback = ["smart","brilliant"] negative_feedback = ["not","poor"] report = ["the student works hard","this student is brilliant"] student_id = [3,1] k = 1
โ€บ Output: [1]
๐Ÿ’ก Note: Student 3: 'works' and 'hard' are neutral words = 0 points. Student 1: 'brilliant' (+3) = 3 points. Return top 1 student.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n*m + s*log(s))

n reports ร— m words for scoring + s students ร— log(s) for sorting

n
2n
โšก Linearithmic
Space Complexity
O(p + s)

p positive/negative words in hash sets + s students in score map

n
2n
โœ“ Linear Space

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
  • No word appears in both positive_feedback and negative_feedback
  • All strings consist of lowercase English letters and spaces
  • Each student_id[i] is unique
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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