A REST API contains information about food outlets across multiple cities. Given the city name and the maximum cost for 2 persons, use the API to get the list of food outlets in this city and have an estimated cost less than or equal to the given cost. The API returns paginated data.
To access the information, perform an HTTP GET request to https://jsonmock.hackerrank.com/api/food_outlets? city=&page= where is the city to get the food outlets for, and is an integer that denotes the page of the results to return.
For example, a GET request to https://jsonmock.hackerrank.com/api/food_outlets?city=Seattle&page=2 returns data associated with the second page of results for Seattle.
Similarly, a GET request to https://jsonmock.hackerrank.com/api/food_outlets?city-Houston&page=1 returns the first page of results for Houston.
The response to such a request is a JSON with the following 5 fields:
- page: the current page of the results.
- per page: the maximum number of records returned per page.
- total: the total number of records in the database.
- total_pages: the total number of pages with results.
- data: either an empty array or an array of outlet objects; each object has the following schema:
- city: queried city where the outlet is located [STRING]
- name: name of the outlet [STRING]
- estimated cost: estimated cost for 2 persons [INTEGER]
- user rating:
- average rating: average rating of the outlet [FLOAT]
- votes: total votes for the outlet [INTEGER]
- id: unique identifier of the outlet [INTEGER]
{
"city": "Houston",
"name": "Cocoa Tree",
"estimated_cost": 10,
"user_rating": {
"average_rating": 4.5,
"votes": 969
},
"id": 938
},
Given a string city, the numerical maximum cost for 2 persons maxCost, return the list of food outlet names that are located in this city and have an estimated cost less than or equal to the given maxCost.
Complete the function getRelevantFoodOutlets in the editor below. Please perform pagination in order to get all the results.
getRelevantFoodOutlets has the following parameter(s):
- string city: string denoting the city of the food outlet
- int maxCost: max cost for 2 people
An array of strings denoting the food outlet names that are located in this city and have an estimated cost less than or equal to maxCost. The names in the array must be ordered as they appear in the API response
The given city will always have data returned from the API.
Note: Please review the header in the code stub to see available libraries for API requests in the selected language. Required libraries can be imported in order to solve the question. Check our full list of supported libraries at https://www.hackerrank.com/ environment.
In the first line, there is a string, city. In the second line, there is an integer, maxCost.
STDIN
Function
→ city = 'Denver'
Denver
50
maxCost = 50
Sample Output
BKC DIVE Vedge
The function makes paginated calls to the API https://jsonmock.hackerrank.com/api/food_outlets?city=Denver&page=1 https://jsonmock.hackerrank.com/api/food outlets?city=Denver&page=2 and so on, to get all the food outlets in Denver city. It returns the outlets for which estimated cost is less than or equal to the given maxCost of 50. The list of outlets is then printed by the stub code.
Solution: https://github.com/Akil-Parker/rest-api-hackerrank-java