FastPrepREST API: Country Codes
Problems/REST API: Country Codes

REST API: Country Codes

HackerRank logoHackerRank● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a HackerRank online assessment for a Forward Deployed Engineer role. One task gave a country and a phone number. You had to get that country's calling code from a test API that answers in JSON, then join the code and the number.

The report does not show the API's reply, the answer format, which code to use when a country has more than one, or what to return when no country matches. Here, the reply is a list of rows, each with a country name and its calling codes. The name must match exactly, the last code in the row is used, and a missing country gives "-1". The answer is "+", then the code, a space and the phone number as given. Your function gets the reply as an input, so it makes no network call and reads no JSON.

Our version is about 75% the same as the reported question.

Return the international form of phoneNumber for the country named country, using the data a countries REST API sent back.

The API's reply is given to you as countryRecords. Each record is one row: the country's name, then one or more calling codes in the order the API lists them. A calling code is the number you dial before a phone number to reach that country, such as 81 for Japan.

Find the record whose name is exactly country, with the same letter case. If there is one, let callingCode be the last calling code in that record, and return "+" + callingCode + " " + phoneNumber. Keep phoneNumber exactly as given, including leading zeros. If no record has that name, return "-1".

Function

getPhoneNumbers(country: String, phoneNumber: String, countryRecords: String[][]) → String

Examples

Example 1

country = "Japan"phoneNumber = "0312345678"countryRecords = [["Germany","49"],["Japan","81"],["Kenya","254"]]return = "+81 0312345678"

The Japan record has the calling code 81. The phone number keeps its leading 0.

Example 2

country = "Dominican Republic"phoneNumber = "5550123"countryRecords = [["Dominica","1767"],["Dominican Republic","1809","1829","1849"]]return = "+1849 5550123"

Dominica is not an exact match, so it is skipped. The Dominican Republic record has three calling codes, and the last one is 1849.

Example 3

country = "Niger"phoneNumber = "80123456"countryRecords = [["Nigeria","234"],["Chad","235"]]return = "-1"

Nigeria starts with Niger but is a different name, and no record is named exactly Niger, so the answer is "-1".

Constraints

  • 0 <= countryRecords.length <= 1000
  • 2 <= countryRecords[i].length <= 6
  • country and every record name countryRecords[i][0] have 1 to 60 characters. They use only English letters, spaces, apostrophes, hyphens, periods, commas and parentheses, and do not start or end with a space.
  • Every calling code countryRecords[i][j] with j >= 1 has 1 to 4 digits and does not start with 0.
  • 1 <= phoneNumber.length <= 15
  • phoneNumber contains only digits.
  • The names in countryRecords are all different.

More HackerRank interview questions

  • Auction Lot Bid SummaryOA · Seen Nov 2026
  • Classify Password StrengthOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Longest Equal Binary SubarrayOA · Seen Dec 2025
  • Longest Equal Binary Subarray After One FlipOA · Seen Dec 2025
  • Longest Prefix-Valid Binary SubarrayOA · Seen May 2025
  • Check Permutation Divisible by EightONSITE INTERVIEW · Seen Mar 2026
  • Ball Passing at Time KPHONE SCREEN · Seen Dec 2025

Similar questions at other companies

  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Find Words That Can Be Formed by CharactersTabaPay · OA
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
  • Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
  • Customer Signer ScreeningState Farm · OA
  • REST API: Highest International StudentsRippling · OARippling interview questions
  • String-Pair Frequency SimilarityIBM · OAIBM interview questions
  • Find Sliding-Window Pattern Start IndicesVisa · OAVisa interview questions
  • Closest ColorTwo Sigma · OATwo Sigma interview questions
  • Number-Character Bijective MappingAdobe · OAAdobe interview questions
All HackerRank interview questionsSee HackerRank hiring insights
Saved
class Solution {
    public String getPhoneNumbers(String country, String phoneNumber, String[][] countryRecords) {
        // Write your code here.
    }
}
Checking account…