179. Largest Number - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given a list of non-negative integers, arrange them such that they form the largest possible number. The result should be returned as a string because the number could be very large.

For example, given the list [3, 30, 34, 5, 9], the largest number formed is "9534330".

Key Idea

The main challenge is to decide how to order any two numbers. We cannot simply sort the numbers in descending order because the relative order depends on how the numbers concatenate. For example:

  • For 3 and 30, if we concatenate them in two possible orders we get:
    • "330" (3 followed by 30)
    • "303" (30 followed by 3)
      Since "330" > "303", 3 should come before 30.

The general approach is to define a custom comparator to compare two numbers (as strings) X and Y by comparing XY with YX.

Detailed Steps

  1. Convert Numbers to Strings:
    Since our comparison is based on string concatenation, first convert each integer into its string representation.

  2. Sort with a Custom Comparator:
    Sort the list of string numbers such that for any two strings a and b, we define:

    • a comes before b if a + b is greater than b + a.
    • Otherwise, b comes before a.
  3. Handle Edge Cases:
    If after sorting the highest number (first element) is "0", it means every number is zero. In that case, return "0".

  4. Concatenate the Sorted Array:
    Join the sorted string numbers to form the final result.

Python Implementation

In Python, we can use functools.cmp_to_key to convert our custom comparator into a key function for sorting.

Python3
Python3

. . . .

Java Implementation

In Java, we implement a custom comparator to sort the string representations of the numbers.

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:

    • Converting numbers to strings takes O(n).
    • Sorting using the custom comparator takes O(n log n), where each comparison involves string concatenation (which in worst-case may take O(k), where k is the average string length).
    • Overall, the complexity is O(n log n * k).
  • Space Complexity:

    • O(n) is needed for storing the string representations.

Common Pitfalls and Edge Cases

  • Leading Zeros:
    Without special handling, inputs like [0, 0] could incorrectly produce "00". Checking the first element after sorting and returning "0" if it's "0" addresses this.

  • Comparator Implementation:
    Ensure the comparator correctly defines the ordering based on the concatenated string values.

  • Large Inputs:
    Be aware of the performance implications when concatenating strings frequently, especially in languages where string concatenation is expensive.

  • Merge Sorted Array:
    A classic problem involving array manipulation.
  • Kth Largest Element in an Array:
    Involves custom sorting and selection techniques.
  • Longest Common Prefix:
    Deals with string manipulation and comparison.
TAGS
LeetCode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How do I describe my industry?
How do I practice programming interviews?
How do you master system design interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;