Largest Number Possible
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Google OA coming up and they're asking you to form the largest number possible from given digits or numbers. This is a classic sorting trick that looks simple until you realize the default sort doesn't work. The catch is in the comparator. You need to rearrange elements so their concatenation is maximized, not just sort them numerically. Google likes this one because it tests whether you can think beyond standard orderings. StealthCoder can feed you the exact comparator logic if you freeze up mid-OA.
Pattern and pitfall
The trap is thinking you can sort digits or numbers in descending order and call it done. That fails on cases like [3, 30, 34, 5, 9]. The real approach: define a custom comparator that compares two elements by their concatenations. For any two candidates a and b, compare the string ab against ba. If ab is lexicographically larger, a comes first. Sort the entire list using this comparator, then concatenate. Edge case: handle leading zeros if the result is all zeros. The pattern is greedy sorting with a non-obvious comparison function. It's not hard once you see it, but the insight doesn't come from intuition alone.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Largest Number Possible 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
This OA pattern shows up on LeetCode as largest number. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google 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.
Largest Number Possible FAQ
Is this really just sorting?+
Yes, but with a custom comparator. You're not sorting by numeric value. You're sorting by which arrangement produces a larger concatenation. That's the whole trick. Once you have the right comparator, the solution is three lines.
What's the gotcha with leading zeros?+
If all input numbers are zero, the output should be a single '0', not '000...'. Check if the first digit of your final concatenation is zero. If it is, return '0'.
How do I know when to use a custom comparator in an interview?+
When the natural sort order doesn't match the problem's goal. Here, you want largest concatenation, not largest numeric value. That mismatch signals a custom comparator. Google tests this intuition.
Can I solve this without converting to strings?+
Technically yes, but string comparison is cleaner and avoids overflow issues with large numbers. Just concatenate as strings and compare lexicographically. That's the intended path.
Is there a mathematical trick or is it pure sorting?+
Pure sorting with a clever comparator. No math, no dynamic programming, no binary search. The elegance is in realizing the comparator is the entire solution.