Rank Student Reports by Whole-Word Feedback Scores

Quick Overview

Rank students by whole-word report scores and return the top k IDs, ordering equal scores by smaller student ID.

Rank Student Reports by Whole-Word Feedback Scores

Company: Booking.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each student begins with zero points. In their report, each occurrence of a word in `positive_feedback` adds 3 points, each occurrence of a word in `negative_feedback` subtracts 1, and any other word adds zero. The feedback word sets do not overlap. Report `i` belongs to `student_id[i]`, and student IDs are unique. Return the top `k` student IDs in decreasing score order, breaking score ties by smaller ID. ```cpp class Solution { public: vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k); }; ``` ### Examples **Example 1:** ```text Input: positive_feedback = ["good"], negative_feedback = ["late"], report = ["good good","good late"], student_id = [5,2], k = 2 Output: [5,2] Explanation: Scores are 6 and 2, so ID 5 ranks first. ``` **Example 2:** ```text Input: positive_feedback = ["good"], negative_feedback = ["late"], report = ["plain","plain"], student_id = [9,3], k = 1 Output: [3] Explanation: Both reports score 0, so the smaller ID wins the tie. ``` ### Constraints - Each feedback array contains from 1 through 10000 lowercase words, each of length 1 through 100. - No word belongs to both feedback arrays. - `1 <= report.length == student_id.length <= 10000`. - Reports use lowercase letters and spaces, have length 1 through 100, and have a single space between consecutive words. - Student IDs are distinct integers in `[1, 10^9]`. - `1 <= k <= report.length`. ### Clarifying Questions - Does a repeated report word score more than once? Yes, once per occurrence. - Does a substring of a word match feedback? No; match whole words. - What breaks equal scores? Smaller student ID. - Are IDs returned in ranked order? Yes. ```hint Check the boundary Check a score tie between two different IDs. ```

Overview: Rank students by whole-word report scores and return the top k IDs, ordering equal scores by smaller student ID.

Booking.com logo
Booking.com
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each student begins with zero points. In their report, each occurrence of a word in positive_feedback adds 3 points, each occurrence of a word in negative_feedback subtracts 1, and any other word adds zero. The feedback word sets do not overlap. Report i belongs to student_id[i], and student IDs are unique.

Return the top k student IDs in decreasing score order, breaking score ties by smaller ID.

class Solution {
public:
    vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k);
};

Examples

Example 1:

Input: positive_feedback = ["good"], negative_feedback = ["late"], report = ["good good","good late"], student_id = [5,2], k = 2
Output: [5,2]
Explanation: Scores are 6 and 2, so ID 5 ranks first.

Example 2:

Input: positive_feedback = ["good"], negative_feedback = ["late"], report = ["plain","plain"], student_id = [9,3], k = 1
Output: [3]
Explanation: Both reports score 0, so the smaller ID wins the tie.

Constraints

  • Each feedback array contains from 1 through 10000 lowercase words, each of length 1 through 100.
  • No word belongs to both feedback arrays.
  • 1 <= report.length == student_id.length <= 10000 .
  • Reports use lowercase letters and spaces, have length 1 through 100, and have a single space between consecutive words.
  • Student IDs are distinct integers in [1, 10^9] .
  • 1 <= k <= report.length .

Clarifying Questions Guidance

  • Does a repeated report word score more than once? Yes, once per occurrence.
  • Does a substring of a word match feedback? No; match whole words.
  • What breaks equal scores? Smaller student ID.
  • Are IDs returned in ranked order? Yes.