Skip to main content

86. Largest Number

mediumAsked at Vercel

Arrange a list of integers to form the largest possible number. Vercel asks this for the custom-comparator insight — same shape as ordering build artifacts by 'which goes first to maximize the final byte-sorted manifest.'

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-12)Vercel platform onsite; custom comparator expected.
  • Blind (2026-Q1)Listed in Vercel screen pool.

Problem

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, you need to return a string instead of an integer.

Constraints

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

Examples

Example 1

Input
nums = [10,2]
Output
"210"

Example 2

Input
nums = [3,30,34,5,9]
Output
"9534330"

Approaches

1. Sort by string descending

Sort strings descending — wrong, '3' > '30' but 330 < 303.

Time
O(n log n)
Space
O(n)
// WRONG: '3' > '30' lex, but the comparator must compare concatenations.

Tradeoff: Fails on the LC example.

2. Sort by concatenation comparator (optimal)

Compare a, b by which (a+b) > (b+a). Sort descending by that. Edge case: all zeros -> return '0'.

Time
O(n log n * k) where k is max digit count
Space
O(n)
function largestNumber(nums) {
  const strs = nums.map(String);
  strs.sort((a, b) => (b + a).localeCompare(a + b));
  if (strs[0] === '0') return '0';
  return strs.join('');
}

Tradeoff: The comparator returns positive when (b+a) > (a+b), placing b before a. The localeCompare returns -1/0/1 — what sort expects. Critical edge case: [0, 0, 0] should return '0', not '000'.

Vercel-specific tips

Vercel grades the comparator design. Bonus signal: proving the comparator induces a total order (it does — transitivity follows from string concatenation ordering being lex-compatible) and handling the all-zeros edge case.

Common mistakes

  • Forgetting the all-zeros edge case — returns '00...0' instead of '0'.
  • Sorting integers numerically — '30' before '3' gives the wrong answer.
  • Using a - b style comparator that returns int — works in C/Java but JS's localeCompare is more robust for strings.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Smallest Number — ascending variant.
  • Reorder Data in Log Files (LC 937) — custom comparator with mixed types.
  • Prove the comparator is transitive.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why concatenation-based comparison?

Lex string comparison fails ('3' > '30'). We need to compare what they LOOK like when placed side by side: '330' vs '303'. The concatenated comparator captures that.

Is the comparator transitive?

Yes. If a+b > b+a and b+c > c+b, then a+c > c+a. Proof relies on the fact that string concatenation order is determined by repeated comparisons of corresponding digit positions, which is consistent.