Data Structures & AlgorithmsHard
Records are (letter, number): (b, 2), (a, 1), (c, 2), (a, 2), (b, 1), (c, 1). Apply a stable sort by LETTER first, then a stable sort by NUMBER. Give the final list of pairs in order, written as letter-number, separated by spaces.
- A. a1 b1 c1 b2 c2 a2
- B. a1 a2 b1 b2 c1 c2
- C. a1 a2 b2 b1 c2 c1
- D. a1 b1 c1 a2 b2 c2
Correct answer: D. a1 b1 c1 a2 b2 c2
Two stable passes give a lexicographic order with the last key dominant: sort by letter first, then by number, and equal numbers keep the letter order. Using the keys in the other order, or a single sort on both keys by tuple, gives different results.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For every window of length 3 in 9 2 7 4 1 8, sort the window and take its MIDDLE value. What is the largest of these middle values?
- Two sorted lists 1 4 6 9 and 2 3 7 8 10 are merged with the standard two-pointer merge (take the smaller head; on a tie take from the…
- In the sorted array 1 2 2 2 3 5 5 8, what is the 0-based index of the FIRST occurrence of the value 5 (the leftmost position where 5…
- How many index pairs i < j in 4 9 2 7 5 3 8 have A[i] + A[j] strictly greater than the MEDIAN of the whole array (the median is the middle…
- Why is the standard binary-search probe count for an unsuccessful search about log₂(n), not n?
- When sorting records by key with counting sort, why must the placement pass proceed in a specific direction for stability?