Data Structures & AlgorithmsHardGoogle · Flipkart interview
Problem: Sort students by grade (descending), breaking ties by name (ascending), using two passes of Python's STABLE sort.
Problem: Sort students by grade (descending), breaking ties by name (ascending), using two passes of Python's STABLE sort.
s = [("Ravi", 82), ("Anu", 91), ("Zoya", 82), ("Kabir", 91), ("Esha", 75), ("Dev", 82)]
s.sort(key=lambda p: p[0])
s.sort(key=lambda p: p[1], reverse=True)
print(" ".join(n for n, _ in s))What is printed?- A. Anu Kabir Dev Ravi Zoya Esha
- B. Kabir Anu Zoya Ravi Dev Esha
- C. Anu Kabir Ravi Zoya Dev Esha
- D. Esha Dev Ravi Zoya Anu Kabir
Correct answer: A. Anu Kabir Dev Ravi Zoya Esha
Sort by the secondary key first, then by the primary key: stability keeps the alphabetical order inside each grade. reverse=True in Python preserves the original order of equal elements (it does not reverse ties), giving Anu Kabir Dev Ravi Zoya Esha.
More Coding Interview Problems questions
- Problem: Find the length of the SHORTEST subarray with sum ≥ k when values may be negative (monotonic deque over prefix sums). python from…
- Problem: Longest subarray whose max − min ≤ limit, using two monotonic deques. python from collections import deque def longest(a, lim)…
- Problem: Count subarrays whose maximum lies in [L, R] in one pass. python def count(a, L, R): lastbad = lastok = -1; res = 0 for i, x in…
- Problem: Shortest path (in steps) from the top-left to the bottom-right cell of a grid where 1 is a wall, if you may remove at most k…
- Problem: A lock has 4 wheels of digits 0–9; one move turns one wheel by ±1 (wrapping 9↔0). Starting at '0000', find the fewest moves to…
- Problem: Water level t floods every cell of height ≤ t. Find the smallest t at which you can swim from the top-left to the bottom-right…