This question runs in a pre-configured Salesforce environment - no personal connection required.

Given an integer topN, find the account names that have the most associated contacts. Use an aggregate SOQL query on the Contact object, grouped by Account Name, ordered by contact count in descending order, and limited to topN results. Return a list of account names ordered from most contacts to fewest. Return an empty list for null or non-positive input.

Only include contacts that have a non-null AccountId in the grouping.

Method signature: public static List<String> accountsWithMostContacts(Integer topN)

Examples:

Input: topN = 3 Output: ['Acme Corp', 'Global Industries', 'TechStart Inc'] Explanation: These three accounts have the most contacts in descending order Input: topN = 1 Output: ['Acme Corp'] Explanation: Returns only the account with the most contacts Input: topN = 0 Output: [] Input: topN = null Output: []
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • This challenge runs without connecting your own org
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay July 2026

Contest runs July 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 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