Sort hotel list challenge

Problem statement
Given a set of hotels and its guests reviews, sort the hotels based on a list of words specified by a user. The criteria to sort the hotels should be how many times the words specified by the user is mentioned in the hotel reviews.


  1. The first line contains a space-separated set of words which we want to find mentions in the hotel reviews.
  2. The second line contains one integer M, which is the number of reviews.
  3. This is followed by M + M lines, which alternates an hotel ID and a review belonging to that hotel.

A list of hotel IDs sorted, in descending order, by how many mentions they have of the words specified in the input. If the count is same, sort according to the hotel IDs.


  • The words to be find will always be singe words line ‘breakfast’ or ‘noise’. Never double words like ‘swimming pool’.
  • Hotel ud is a 4-byte integer.
  • Words match should be case-insensitive.
  • Dots and commas should be ignored.
  • If a word appears in a review twice, it should count twice.
  • If two hotels have the same number of mentions, they should be sorted in the output based on their ID, smallest ID first.
  • In case one or more test cases time out, consider revisiting the runtime complexity of your algorithms.

Sample input

breakfast beach citycenter location metro view staff price
This hotel has a nice view of the citycenter. The location is perfect.
The breakfast is ok. Regarding the location, it is quite far from citycenter but price is cheap so it is worth.
Location is excellent, 5 minutes from citycenter. There is also a metro station very close to the hotel.
They said I could't take my dog and there were other guests with dogs! That is not fair!
Very friendly staff and goof cost-benefit ratio. Its location is a bit far from citycenter.

Sample Output

2 1

Hotel 2 has 7 mentions of the words: ‘location’ and ‘citycenter’ are mentioned twice while ‘breakfast’, ‘price’ and ‘staff’ are mentioned once. Hotel 1 in the other hand has 6 mentions in total ‘location’ and ‘citycenter’ also twice and then ‘view’ and ‘metro’ once.

'Coz sharing is caring

By Swatantra Kumar

Swatantra is an Open Source evangelist, a technologist and researcher. Professionally, he does software development, software architecture, server administration and project management. When he's not writing software, he enjoys building web entities and servers, reading about and working with new technologies, and trying to get his friends to make the move to open source software. He's written, co-written and published many articles in international journals, on various domains/topics including Open Source, Networks, Computer Organization, Mobile Technologies, and Business Intelligence. He made a proposal for an information management system at University level during graduation days.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.