Data Structures & AlgorithmsModerateFlipkart/Uber OA · placement
Records are sorted with a composite key. What is printed?
Records are sorted with a composite key. What is printed?
recs = [('kabir', 80), ('dev', 70), ('meera', 70), ('ravi', 70), ('zoya', 80), ('anu', 80)]
recs.sort(key=lambda r: (-r[1], r[0]))
print([r[0] for r in recs])- A. ['kabir', 'anu', 'zoya', 'dev', 'meera', 'ravi']
- B. ['zoya', 'kabir', 'anu', 'ravi', 'meera', 'dev']
- C. ['anu', 'zoya', 'kabir', 'dev', 'meera', 'ravi']
- D. ['anu', 'kabir', 'zoya', 'dev', 'meera', 'ravi']
Correct answer: D. ['anu', 'kabir', 'zoya', 'dev', 'meera', 'ravi']
Negating the score sorts it descending while names stay ascending; reverse=True would also reverse the name order within equal scores. Output: ['anu', 'kabir', 'zoya', 'dev', 'meera', 'ravi'].
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
- Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. The span of day i is the number of consecutive days ending at day i…
- You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?
- The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] are sorted by start and merged (intervals that touch, like [2, 4] and…
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [17…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [2…
- Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [14, 4, 8, 46, 16, 34, 31, 40] and k = 3…