FastPrepTop Three Companies by Profit
Problems/Top Three Companies by Profit

Top Three Companies by Profit

Cresta logoCresta● EasyFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source gives the aggregation and top-three objective but omits the input representation, tie rule, bounds, and return behavior for small inputs. The judged core task matches the visible source at about 91%.

You are given unique company names in companyNames. For each index i, dailyProfits[i] contains that company's integer profit values over several days.

Compute each company's total profit and return the names of the companies with the three highest totals. Order the result by total profit descending. If totals are equal, order those companies by name lexicographically ascending. If fewer than three companies are provided, return every company in that order.

Function

topThreeCompanies(companyNames: String[], dailyProfits: long[][]) → String[]

Examples

Example 1

companyNames = ["Orion","Nova","Atlas","Zen"]dailyProfits = [[3,5],[10,-2],[4,4],[1,1]]return = ["Atlas","Nova","Orion"]

Atlas, Nova, and Orion each total 8. Their equal totals are resolved lexicographically, so Atlas comes before Nova, then Orion.

Example 2

companyNames = ["Beta","Alpha"]dailyProfits = [[-5],[-1,0]]return = ["Alpha","Beta"]

Alpha totals -1 and Beta totals -5. Because only two companies are provided, both are returned.

Example 3

companyNames = ["A","B","C","D","E"]dailyProfits = [[9],[-2,20],[5,5],[100,-100],[8]]return = ["B","C","A"]

The totals are 9, 18, 10, 0, 8. Therefore B, C, and A occupy the first three ranks.

Constraints

  • 1 <= companyNames.length == dailyProfits.length <= 10^5.
  • Every company name is unique, has length from 1 through 30, and contains only English letters.
  • The total number of entries across dailyProfits is at most 10^5.
  • -10^9 <= dailyProfits[i][j] <= 10^9.
  • Use 64-bit arithmetic for company totals.

More Cresta problems

  • File Storage ServicePHONE SCREEN · Seen Nov 2026
  • Decode Words with Anagram KeysOA · Seen Oct 2026
  • Find a File in a Directory SnapshotOA · Seen Oct 2026
All Cresta interview questionsSee Cresta hiring insights
Saved
class Solution {
    public String[] topThreeCompanies(String[] companyNames, long[][] dailyProfits) {
      // write your code here
    }

}
Checking account…