MediumArrayStringGreedySortingUpdated Sep 2026

179. Largest Number

Asked at Adobe

Difficulty

Medium

Time

O(n log n * k)

Space

O(n * k)

Pattern

Array

Companies

1

Problem

You are given a list of non-negative integers. Rearrange all of them in some order and join them together to build one big number. Return the arrangement that gives the largest possible number.

Because the answer can be far too big for a normal integer type, return it as a string, not a number.

Every element must be used exactly once. The order of the input list does not matter — you may place the numbers in any order you like. One special case: if every number is 0, the joined result would be something like "000", but the correct answer is just "0".

Order changes the value

nums10201Input: the numbers 10 and 2join A10201210 then 2 gives "102"join B2100122 then 10 gives "210" — larger, so this is the answer
The same two numbers give "102" or "210" depending on the order, and we want the bigger one.

Constraints

  • ●1 <= nums.length <= 100
  • ●0 <= nums[i] <= 10^9

Examples

Example 1
Inputnums = [10,2]
Output"210"
WhyThe two possible joins are "102" and "210". "210" is bigger, so we put 2 first and 10 second.
Example 2
Inputnums = [3,30,34,5,9]
Output"9534330"
WhyPutting the biggest leading digit first gives 9, then 5, then 34, then 3, then 30. Joining them reads "9534330", which beats every other ordering.
Example 3
Inputnums = [0,0]
Output"0"
WhyBoth numbers are zero, so the join is "00". We return the single string "0" instead.

Asked At

1 companies
CompanyDifficulty
AdobeMediumView all Adobe questions →

How to Think About It

1

Brute force: Generate all permutations and compare concatenated results — O(n! * n) time.

2

Greedy insight: Define a custom comparator where a sorts before b if ab > ba.

3

Convert all integers to strings to avoid integer overflow during concatenation.

4

After sorting, concatenate and handle the edge case where the result is all zeros.

5

Optimal: Custom sort with string comparison comparator — O(n log n * k) time where k is avg digit length.

Optimal Approach

Convert all numbers to strings and sort them with a custom comparator: for two strings a and b, a should come before b if a+b > b+a lexicographically. Concatenate the sorted array. If the result starts with '0', return '0' (all elements were zero). This greedy approach guarantees the largest possible concatenation because the comparator is transitive and captures the exact ordering needed. Time is O(n log n * k) where k is the average string length.

Sort with the a+b vs b+a rule

as strings330345901234Step 1 — turn every number into a stringcompare a+b with b+abigger glue goes firstsort all pairs this wayStep 2 — the custom comparator decides the ordersorted953433001234Step 3 — sorted result, ready to joinjoin → "9534330"if result[0] == '0': return "0"
Compare two numbers by gluing them both ways; the bigger glue goes first.
ComplexityTime O(n log n * k)Space O(n * k)Sorting does O(n log n) comparisons, and each comparison builds two strings of length up to k; the extra space holds the string copies of all numbers.

What Trips People Up in Real Interviews

1

Clarify that the output is a string, not an integer, since it can exceed 64-bit range.

2

Explain the comparator: for strings a and b, compare a+b vs b+a lexicographically.

3

Handle the edge case [0, 0, 0] → return "0", not "000".

4

Mention that Python's sort is stable, but the comparator handles all cases anyway.

5

Walk through [10, 2] → "210" since "210" > "102" to verify your comparator logic.

Solution Code

O(n log n * k) time
from functools import cmp_to_key
class Solution:
    def largestNumber(self, nums):
        strs = [str(n) for n in nums]
        strs.sort(key=cmp_to_key(lambda a, b: 1 if a + b < b + a else -1))
        result = ''.join(strs)
        return '0' if result[0] == '0' else result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Largest Number problem?

Given a list of non-negative integers, arrange them such that they form the largest possible number and return it as a string. For example, [3, 30, 34, 5, 9] should return "9534330". The result may be extremely large and cannot fit in a 32-bit integer.

How do you solve Largest Number?

The optimal approach runs in O(n log n * k) time and O(n * k) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Largest Number?

Largest Number is asked at Adobe. It is a medium difficulty problem.

What are common mistakes on Largest Number?
  • Clarify that the output is a string, not an integer, since it can exceed 64-bit range.
  • Explain the comparator: for strings a and b, compare a+b vs b+a lexicographically.
  • Handle the edge case [0, 0, 0] → return "0", not "000".
  • Mention that Python's sort is stable, but the comparator handles all cases anyway.
  • Walk through [10, 2] → "210" since "210" > "102" to verify your comparator logic.