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
- A. 3430953
- B. 3303459
- C. 9534330
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Prices by day: 1, 5, 3, 8, 6, 10. Profit is earned in every rise: whenever tomorrow's price is higher than today's, buy today and sell…
- Array a = [3, 1, 2, 5, 4] is sorted by bubble sort: in each pass adjacent pairs out of order are swapped (a[j] > a[j+1]). What is the…
- Items of weights 7, 3, 5, 2, 6 are considered in increasing weight order. Each item is taken if it still fits in the capacity 13. What is…
- For i from 0 to 30 inclusive, count the values of i with ii <= 30. What is printed? count = 0 for i = 0 to 30 if i i <= 30 then count =…
- Array [4, 1, 3, 2] is sorted by selection sort: for each position i, find the minimum of the unsorted part and swap it into position i…
- Sorted array a = [1, 2, 3, 5, 8, 9] and limit L = 10. Count the pairs of distinct positions i < j with a[i] + a[j] <= L using two…