Concatenate Numeric Shard Labels Into the Largest Possible Key
Last updated: November 29, 2026
Quick Overview
Given a list of non-negative integer shard labels, arrange them so that concatenating them forms the largest possible number, and return it as a string.
0
0
0 solved
Given a list of non-negative integer shard labels, arrange them so that concatenating them forms the largest possible number, and return it as a string.
Google screens use this to test custom comparators, a spot where many candidates sort by value or by string and get subtly wrong answers. The interviewer checks that you derive the pairwise rule a+b versus b+a and can say why it defines a consistent order.
What the Interviewer Expects
- Reject naive numeric and lexicographic sorts with counterexamples
- Compare pairs by which concatenation order is larger
- Implement with functools.cmp_to_key
- Handle the all-zeros case
- Mention why the comparator is transitive
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the smallest number instead.
- Labels are huge (thousands of digits each); how does the comparator cost change?
- Prove transitivity of the a+b > b+a relation.
- Only k of the labels may be used; which ones and in what order?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of non-negative ints. Output: the largest number formed by concatenating all of them in some order, as a string without redundant leading zeros (so [0, 0] gives "0")....
High-Level Design
Plain descending numeric order fails: [3, 30] must give 330, while descending numeric order gives 303. Lexicographic order also fails on prefix cases like [121, 12], where 12121 beats `12112...