Point OA 面试真题解析:Finest Food Outlet(分页 API + 排序筛选)

20次阅读
没有评论

Complete the finestFoodOutlet function below.

The function is expected to return a STRING.

The function accepts the following parameters:

  • STRING city
  • INTEGER votes

API URL: https://jsonmock.hackerrank.com/api/food_outlets?city={city}&page={page_no}

Use the HTTP GET method to retrieve information from a database of food outlets. Query https://jsonmock.hackerrank.com/api/food_outlets?city=<city> to find all the records for a city. The query result is paginated. To access additional pages, append &page=<num> to the URL where num is the page number.

The response is a JSON object with the following fields:

  • page: The current page of the results.
  • per_page: The maximum number of results returned per page.
  • total: The total number of results.
  • total_pages: The total number of pages with results.
  • data: Either an empty array or an array with a single object that contains the food outlets’ records.

In data, each food outlet has the following schema:

  • id: outlet id
  • name: The name of the outlet
  • city: The city in which the outlet is located
  • estimated_cost: The estimated cost of the food in the particular outlet
  • user_rating: An object containing the user ratings for the outlet
    • average_rating: The average user rating for the outlet
    • votes: The number of people who voted for the outlet

Given the city name as city and minimum vote count as votes, filter the results by city name. Find the food outlet with the highest rating and whose vote count is greater than or equal to the required minimum votes. In case of a tie in the rating, return the one with the maximum vote count.

Function Description

Complete the function finestFoodOutlet in the editor below.

finestFoodOutlet has the following parameter(s):

  • string city: name of the city whose outlets have to be filtered
  • integer votes: number of votes

Returns

string: the winning restaurant

Input Format For Custom Testing

The first line contains the string, city.

The second line contains an integer, votes.

Sample Input For Custom Testing

Seattle
500

Sample Output

Cafe Juanita

Explanation

In Seattle, results are filtered to those with votes >= 500. There are 4 food outlets whose rating is 4.9. Cafe Juanita has 16203 votes.

这道题本质上是一次“分页 API 拉取 + 过滤 + 排序”的综合题。需要根据给定城市调用接口,遍历所有分页数据,筛选出城市匹配且 <code>user_rating.votes</code> 不小于阈值的餐厅,然后按 <code>average_rating</code> 选最高者;如果评分相同,再按投票数选择更大的那个。实现时重点在于正确处理分页,收集全部记录后再统一比较,避免只看第一页数据。

正文完
 0