Get Largest Number
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
JP Morgan asked this in August 2024, and it's deceptively simple on the surface. You're given a list of non-negative integers and need to arrange them to form the largest possible number when concatenated as a string. The trick isn't math, it's comparison. Most candidates sort numerically and fail. The OA is testing whether you can flip your thinking from numeric order to lexicographic order based on concatenation. StealthCoder sits ready if you blank on the comparator logic during the live assessment.
Pattern and pitfall
The pattern here is sorting with a custom comparator, not standard numeric sorting. The key insight: for two numbers a and b, you don't compare a < b or a > b directly. Instead, you compare the concatenated strings ab versus ba. If ba is lexicographically larger, b should come before a in your result. This is a greedy approach that guarantees the largest final number. The pitfall is trying to use standard numeric sorts or attempting to pad digits. You need to treat each number as a string, define a custom comparison function, and sort by which concatenation is greater. Edge cases: single element, all zeros (output should be '0', not '000'), and very large numbers. StealthCoder will show you the pattern instantly if you're stuck during the OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Get Largest Number 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 JP Morgan's OA.
JP Morgan reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Largest Number FAQ
Do I need to handle negative numbers?+
The problem states non-negative integers, so no. All inputs are zero or positive. Focus only on arranging positive values and the edge case where all inputs are zero.
What's the actual trick here?+
The trick is the custom comparator: compare concatenations, not numeric values. For numbers a and b, if the string ab is lexicographically larger than ba, a comes first. This greedy approach always yields the maximum result.
How do I handle the all-zeros case?+
If the final sorted list starts with 0, the answer is just '0', not multiple zeros. Check the first character of your result after sorting and concatenating.
Is this a hard pattern to spot in 60 seconds?+
Yes, most people try numeric sorting first and fail. The insight that concatenation-based comparison beats numeric order takes a moment to click. That's why knowing the pattern beforehand matters.
What's the runtime complexity I should target?+
O(n log n) for sorting, where each comparison is O(k) for string concatenation (k is digit count). This is optimal and acceptable. Don't overthink optimizing further during the OA.