Top Three Companies by Profit
Reported by candidates from Cresta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Cresta's OA, reported July 2026, hands you up to 10^5 companies and up to 10^5 total profit entries. Sorting everything works, but the real question is whether you notice you only need the top three. Nobody's asking for a full ranking. It's a sum-and-rank problem with a tiebreak on name, and the traps are overflow and the tie order. If you blank on the comparator mid-assessment, StealthCoder is the invisible safety net that reads the screen and gives you a working solution. Otherwise, here's the script.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sum each company's profits into a 64-bit integer. Values reach 10^9 in magnitude and there can be 10^5 entries, so totals can hit 10^14. A 32-bit int will silently break. Then rank by total descending and name ascending on ties. The simple route is sorting all indices with that comparator, O(n log n), which is fine at 10^5. The tighter route is a single pass keeping a best-three list, or a size-3 heap, which is O(n). Pitfalls: comparing totals by subtraction (overflow risk, use proper comparison), using locale-aware string compare instead of plain lexicographic, and forgetting that fewer than three companies means return them all. Negative totals still rank normally. If you freeze on the comparator during the live OA, StealthCoder is the hedge that gives you the working code fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Top Three Companies by Profit cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Cresta's OA.
Cresta reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top Three Companies by Profit FAQ
What's the trick in the Cresta top three companies problem?+
Sum each row into a long, then sort by total descending with name ascending as the tiebreak. Take the first three. The only real traps are 32-bit overflow and getting the tie order backwards. Everything else is bookkeeping.
Do I need a heap or is sorting enough?+
Sorting is enough. With n up to 10^5, O(n log n) is comfortable. A single pass tracking the best three is faster and shows polish, but it's not required. Pick whichever you can write without bugs under pressure.
Why does the problem say to use 64-bit arithmetic?+
One value can be 10^9 in magnitude, and a company could have up to 10^5 entries. That puts totals around 10^14, far past the 32-bit limit of about 2.1 billion. Use long in Java or a 64-bit type in your language.
How do ties get handled?+
Companies with equal totals are ordered by name lexicographically ascending. In Example 1, Atlas, Nova, and Orion all total 8, so they come out in that alphabetical order. Names are unique, so the comparator is never ambiguous.
How do I prepare for this in 48 hours?+
Practice one custom comparator sort and one top-k pass. Write a solution that handles fewer than three companies and negative totals, as Example 2 shows. Test with all-equal totals and a single company. That covers nearly every failure mode.