Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
Records are sorted with a composite key. What is printed?
Records are sorted with a composite key. What is printed?
recs = [('anu', 80), ('zoya', 70), ('dev', 70), ('ravi', 70), ('meera', 90), ('kabir', 70)]
recs.sort(key=lambda r: (-r[1], r[0]))
print([r[0] for r in recs])- A. ['meera', 'anu', 'dev', 'kabir', 'ravi', 'zoya']
- B. ['meera', 'anu', 'zoya', 'ravi', 'kabir', 'dev']
- C. ['anu', 'meera', 'dev', 'kabir', 'ravi', 'zoya']
- D. ['meera', 'anu', 'dev', 'ravi', 'kabir', 'zoya']
Correct answer: A. ['meera', 'anu', 'dev', 'kabir', 'ravi', 'zoya']
Negating the score sorts it descending while names stay ascending; reverse=True would also reverse the name order within equal scores. Output: ['meera', 'anu', 'dev', 'kabir', 'ravi', 'zoya'].
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
- Two sorted arrays of sizes m and n (m ≤ n) are given. What is the best possible time to find the median of their union?
- A circular queue in an array of size n uses front and rear indices and keeps one slot always empty. What is the maximum number of elements…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "bbaabbbabaaa", "aa" over = sum(1 for i in…
- A sorted array was rotated to give [35, 45, 52, 68, 78, 1, 5, 12, 18, 27, 28]. Binary search finds the index of the minimum: while lo <…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 5 positions and then LEFT by 7 positions. What is the final array?
- Keys 28, 19, 53, 91, 92, 20, 74, 89 are inserted in this order into an empty hash table of size 10 with h(k) = k mod 10 and linear…