Join Numbers into the Largest Value
Updated
Problem statement
You are given an array of non-negative integers nums. Write each value in base 10 without leading zeros, put the values in some order, and join them into one long string of digits.
Return the largest number you can make this way, as a string. If the number is 0 (every value is 0), return "0", not a string of several zeros.
The answer can be far longer than any built-in integer type, which is why it is returned as a string.
Function
largestNumber(nums: int[]) → StringExamples
Example 1
nums = [7,76,768]return = "776876"Compare two values by joining them both ways. 7 before 76 gives 776, and 76 before 7 gives 767, so 7 goes first. 768 before 76 gives 76876, and 76 before 768 gives 76768, so 768 goes before 76. The order 7, 768, 76 gives "776876".
Example 2
nums = [0,0,0,0]return = "0"Joining gives "0000", but the number is just 0, so return "0".
Example 3
nums = [50,5,9,512]return = "9551250"9 comes first. 5 goes before 50 because 550 is larger than 505, and 512 goes before 50 because 51250 is larger than 50512. The order 9, 5, 512, 50 gives "9551250".
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 10^9
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Return Exact Cash Register ChangeBank of America · OABank of America interview questions
- Largest Binary Number by ConcatenationAdobe · OAAdobe interview questions
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Swap Parity (Data Scientist / Analytics :)JP Morgan Chase · OAJP Morgan Chase interview questions
- Doing Smart WorkRubrik · OARubrik interview questions
- Swap ParityPalantir · OAPalantir interview questions
- Travelers per Consecutive City TripletUber · ONSITE INTERVIEWUber interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)