REST API: Country Codes
Updated
Problem statement
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[][]) → StringExamples
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 <= 10002 <= countryRecords[i].length <= 6countryand every record namecountryRecords[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]withj >= 1has 1 to 4 digits and does not start with0. 1 <= phoneNumber.length <= 15phoneNumbercontains only digits.- The names in
countryRecordsare 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