Problem · String

REST API: Country Codes

Learn this problem
EasyPalantirINTERNOA

Problem statement

Given a country name and a phone number, query the API at https://jsonmock.hackerrank.com/api/countries?name=country to get the country's calling codes. Prepend the calling code to the phone number and return the string. If the data array is empty, return the string '-1'. If there are multiple calling codes, use the one at the highest index. The format of the number should be:

<Calling Code><space><Phone Number>

Example:

+1 7653555443

The response is a JSON object with 5 fields. The essential field is data:

  • data: Either an empty array or an array with a single object that contains the country's record.
  • In the data array, the country has the following schema:

  • name: The name of the country (String)
  • callingCodes: An array of the country's calling codes (String Array)
  • A number of fields that are not of interest.
  • page, per_page, total, total_pages, etc. are not required for this task.

    If the country is found, the data array contains exactly 1 element. If not, it is empty and the function should return '-1'.

    Function

    getPhoneNumbers(country: String, phoneNumber: String) → String

    Complete the getPhoneNumbers function in the editor.

    getPhoneNumbers has the following parameters:

    • string country: the country to query
    • string phoneNumber: the phone number

    Returns

    string: the completed phone number or -1

    Examples

    Example 1

    country = "Afghanistan"phoneNumber = "6564454455"return = "+93 6564454455"
    A call is made to API https://jsonmock.hackerrank.com/api/countries?name=Afghanistan. The calling codes array contains 1 entry, '93'.

    Example 2

    country = "Puerto Rico"phoneNumber = "564539386"return = "+1939 564539386"
    A call is made to the API to fetch the record for Puerto Rico. The returned callingCodes = ['1787', '1939']. Use the higher index code, callingCodes[1].

    Example 3

    country = "Oceania"phoneNumber = "987574876"return = "-1"
    The API call return has an empty data array.

    Constraints

  • The returned JSON object contains either 0 or 1 record in data.
  • he country name may contain uppercase and lowercase English letters and <space> (ascii 32)
  • More Palantir problems

    drafts saved locally
    public String getPhoneNumbers(String country, String phoneNumber) {
        // write your code here
    }
    
    country"Afghanistan"
    phoneNumber"6564454455"
    expected"+93 6564454455"
    checking account