Rank hotels by keyword review score and return the top k

Quick Overview

Score hotels from their guest reviews by adding 3 for each positive keyword and subtracting 1 for each negative keyword, then return the ids of the top k hotels with ties broken by smaller id. It tests hash set lookups, per-key aggregation in a hash map, and sorting with a two-level ordering rule.

Rank hotels by keyword review score and return the top k

Company: Booking.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two lists of strings, `positive_keywords` and `negative_keywords`, a list of integers `hotel_ids`, a list of strings `reviews`, and an integer `k`. The review `reviews[i]` was given to the hotel with id `hotel_ids[i]`. Each hotel has a score that starts at 0. For each review of a hotel, scan the words of the review and update that hotel's score as follows: - Add 3 to the score for every word that appears in `positive_keywords`. - Subtract 1 from the score for every word that appears in `negative_keywords`. Return a list containing the ids of the top `k` hotels sorted in descending order by score. If two hotels have the same score, the hotel with the smaller id comes first. ### Function Signature ```python def top_k_hotels( positive_keywords: list[str], negative_keywords: list[str], hotel_ids: list[int], reviews: list[str], k: int, ) -> list[int]: ``` ### Rules - Words in a review are separated by single spaces. A word matches a keyword only when the whole word equals it, so `uncool` does not count as `cool`. - A keyword counts once per occurrence: a review that contains `clean` twice adds 6. - A hotel with several reviews accumulates the points from all of them. - Every id that appears in `hotel_ids` takes part in the ranking, including hotels whose score is 0 or negative. - Order by score descending, then by hotel id ascending, and return exactly `k` ids. ### Constraints - `1 <= len(positive_keywords) <= 500` - `1 <= len(positive_keywords[i]) <= 20` - `1 <= len(negative_keywords) <= 500` - `1 <= len(negative_keywords[i]) <= 20` - No word appears in both `positive_keywords` and `negative_keywords`. - `1 <= len(hotel_ids) == len(reviews) <= 1000` - `1 <= hotel_ids[i] <= 10^6` - `1 <= len(reviews[i]) <= 1000` - `1 <= k <=` the number of distinct ids in `hotel_ids` - All keywords and review words consist of lowercase English letters. ### Examples ```text Input: positive_keywords = ["cool","clean","quiet"], negative_keywords = ["dirty","loud"], hotel_ids = [1,2,1,3,2], reviews = ["this hotel was cool and clean","very dirty and loud","quiet and clean stay","cool quiet clean place","loud but clean"], k = 2 Output: [1,3] ``` Hotel 1 gets 6 from "cool" and "clean" in its first review and 6 from "quiet" and "clean" in its second, for 12. Hotel 2 gets -2 from "dirty" and "loud", then +2 from "loud" and "clean", for 0. Hotel 3 gets 9 from "cool", "quiet" and "clean". The ranking is 1 (12), 3 (9), 2 (0), so the top two are [1,3]. ```text Input: positive_keywords = ["great"], negative_keywords = ["bad"], hotel_ids = [7,8,9], reviews = ["great great","bad","great bad"], k = 3 Output: [7,9,8] ``` Hotel 7 scores 6 because "great" appears twice. Hotel 9 scores 3 - 1 = 2 and hotel 8 scores -1. With `k = 3` all three hotels are returned in score order. ```text Input: positive_keywords = ["good"], negative_keywords = ["poor"], hotel_ids = [4,2,9], reviews = ["good","good","goodness poor"], k = 2 Output: [2,4] ``` Hotels 4 and 2 both score 3, so the smaller id 2 comes first. "goodness" is not the keyword "good", so hotel 9 only gets -1 from "poor" and misses the top two.

Overview: Score hotels from their guest reviews by adding 3 for each positive keyword and subtracting 1 for each negative keyword, then return the ids of the top k hotels with ties broken by smaller id. It tests hash set lookups, per-key aggregation in a hash map, and sorting with a two-level ordering rule.

Booking.com logo
Booking.com
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two lists of strings, positive_keywords and negative_keywords, a list of integers hotel_ids, a list of strings reviews, and an integer k. The review reviews[i] was given to the hotel with id hotel_ids[i].

Each hotel has a score that starts at 0. For each review of a hotel, scan the words of the review and update that hotel's score as follows:

  • Add 3 to the score for every word that appears in positive_keywords .
  • Subtract 1 from the score for every word that appears in negative_keywords .

Return a list containing the ids of the top k hotels sorted in descending order by score. If two hotels have the same score, the hotel with the smaller id comes first.

Function Signature

def top_k_hotels(
    positive_keywords: list[str],
    negative_keywords: list[str],
    hotel_ids: list[int],
    reviews: list[str],
    k: int,
) -> list[int]:

Rules

  • Words in a review are separated by single spaces. A word matches a keyword only when the whole word equals it, so uncool does not count as cool .
  • A keyword counts once per occurrence: a review that contains clean twice adds 6.
  • A hotel with several reviews accumulates the points from all of them.
  • Every id that appears in hotel_ids takes part in the ranking, including hotels whose score is 0 or negative.
  • Order by score descending, then by hotel id ascending, and return exactly k ids.

Constraints

  • 1 <= len(positive_keywords) <= 500
  • 1 <= len(positive_keywords[i]) <= 20
  • 1 <= len(negative_keywords) <= 500
  • 1 <= len(negative_keywords[i]) <= 20
  • No word appears in both positive_keywords and negative_keywords .
  • 1 <= len(hotel_ids) == len(reviews) <= 1000
  • 1 <= hotel_ids[i] <= 10^6
  • 1 <= len(reviews[i]) <= 1000
  • 1 <= k <= the number of distinct ids in hotel_ids
  • All keywords and review words consist of lowercase English letters.

Examples

Input: positive_keywords = ["cool","clean","quiet"], negative_keywords = ["dirty","loud"], hotel_ids = [1,2,1,3,2], reviews = ["this hotel was cool and clean","very dirty and loud","quiet and clean stay","cool quiet clean place","loud but clean"], k = 2
Output: [1,3]

Hotel 1 gets 6 from "cool" and "clean" in its first review and 6 from "quiet" and "clean" in its second, for 12. Hotel 2 gets -2 from "dirty" and "loud", then +2 from "loud" and "clean", for 0. Hotel 3 gets 9 from "cool", "quiet" and "clean". The ranking is 1 (12), 3 (9), 2 (0), so the top two are [1,3].

Input: positive_keywords = ["great"], negative_keywords = ["bad"], hotel_ids = [7,8,9], reviews = ["great great","bad","great bad"], k = 3
Output: [7,9,8]

Hotel 7 scores 6 because "great" appears twice. Hotel 9 scores 3 - 1 = 2 and hotel 8 scores -1. With k = 3 all three hotels are returned in score order.

Input: positive_keywords = ["good"], negative_keywords = ["poor"], hotel_ids = [4,2,9], reviews = ["good","good","goodness poor"], k = 2
Output: [2,4]

Hotels 4 and 2 both score 3, so the smaller id 2 comes first. "goodness" is not the keyword "good", so hotel 9 only gets -1 from "poor" and misses the top two.