REST API: Discounted Price
Learn this problemProblem statement
Query an API to retrieve a product’s discounted price based on its barcode.
The API endpoint is: https://jsonmock.hackerrank.com/api/inventory?barcode={barcode}, replace {barcode}. The response contains a data field, which is either:
- An empty array (barcode not found)
- An array with a single object containing the item's record
For a barcode found in the system, calculate the discounted price using:
discountedPrice = price - ((discount / 100) × price)Round the result to the nearest integer. If the barcode is not found, return -1.
Example barcode = 74001755
Querying https://jsonmock.hackerrank.com/api/inventory?barcode=74001755 returns a data array with this record:
{
"barcode": "74001755",
"item": "Ball Gown",
"category": "Full Body Outfits",
"price": 785,
"discount": 7,
"available": 1
}discountPrice = 785 - (7/100 * 785) = 730.05 which rounds to 730.
Function Description Complete the getDiscountedPrice function in the editor with the following parameter:
int barcode: the item to queryReturns int: the discounted price rounded to the nearest integer or -1
Examples
Example 1
placeholder = "read-only"return = "not-applicable"Placeholder only: This sample input, output, answer, and test result exist only for workspace compatibility. They do not evaluate the source task.
Constraints
- There will be either 1 or 0 records in data. 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