ProgrammingVery hardInfosys · Cognizant

Five numbers 3, 30, 34, 5, 9 are to be arranged to form the largest possible number. Two numbers x, y are put in the order that gives the larger string xy versus yx, and the strings are concatenated in that sorted order. What is printed?

Five numbers 3, 30, 34, 5, 9 are to be arranged to form the largest possible number. Two numbers x, y are put in the order that gives the larger string xy versus yx, and the strings are concatenated in that sorted order. What is printed?
sort strings with comparator: x before y if x+y > y+x
print concatenation
  1. A. 3430953
  2. B. 3303459
  3. C. 9534330
  4. D. 9533034

Correct answer: C. 9534330

Comparing xy with yx decides order: 34 beats 3 because 343 > 334, and 3 beats 30 because 330 > 303. Plain numeric descending gives 3430953, ascending gives 3303459, and sorting only by leading digit gives 9533034.

More Pseudocode and Code Tracing questions

Exams that ask this topic