Write a method that takes a language name and makes an HTTP GET callout to the REST Countries API to find all countries where that language is spoken. Return a list of country common names, sorted alphabetically. Return an empty list if the language is not found, null, or empty, or if the API call fails.

API Endpoint: https://restcountries.com/v3.1/lang/{language}?fields=name

  • Returns a JSON array of country objects
  • Each object has a name field with a common sub-field

Method Signature:

public static List<String> fetchCountriesByLanguage(String language)

Examples:

Input: language = 'swedish' Output: ['Finland', 'Sweden'] Explanation: The API returns countries where Swedish is spoken, sorted alphabetically Input: language = 'spanish' Output: ['Argentina', 'Bolivia', 'Chile', 'Colombia', 'Costa Rica', 'Cuba', 'Dominican Republic', 'Ecuador', 'El Salvador', 'Equatorial Guinea', 'Guatemala', 'Honduras', 'Mexico', 'Nicaragua', 'Panama', 'Paraguay', 'Peru', 'Spain', 'Uruguay', 'Venezuela'] Explanation: Spanish is spoken in many countries, all sorted alphabetically Input: language = 'invalidlanguage' Output: [] Explanation: API returns 404 status for non-existent languages Input: language = null Output: [] Explanation: Null input returns empty list Input: language = '' Output: [] Explanation: Empty string returns empty list
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In