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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Mar/April 2026

Contest runs March 3 - April 30. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.