Data Structures & AlgorithmsEasyAccenture · Cognizant coding round pattern
Problem: Orders (name, priority) are sorted by priority only, using Python's sorted with a key.
Problem: Orders (name, priority) are sorted by priority only, using Python's sorted with a key.
orders = [("ravi", 3), ("asha", 1), ("john", 3), ("mei", 2), ("ola", 1)]
print([n for n, p in sorted(orders, key=lambda t: t[1])])What is printed?- A. ['asha', 'ola', 'mei', 'john', 'ravi']
- B. ['ola', 'asha', 'mei', 'john', 'ravi']
- C. ['asha', 'ola', 'mei', 'ravi', 'john']
- D. ['ravi', 'john', 'mei', 'asha', 'ola']
Correct answer: C. ['asha', 'ola', 'mei', 'ravi', 'john']
Python's sort (Timsort) is stable: records with equal keys keep their original relative order. Priority 1: asha before ola; priority 3: ravi before john, exactly as in the input. Alphabetical tie-breaking would need key=(p, name). O(n log n).
More Coding Interview Problems questions
- Problem: A juice stall sells a ₹5 cup; customers pay with ₹5, ₹10 or ₹20 notes and the stall starts with no change. Decide whether every…
- Problem: Builds 1..100 are tested; every build from some version onward is broken. isbad() is expensive, so find the first bad build with…
- Problem: Sort values by increasing frequency; values with the same frequency go in decreasing order. python from collections import…
- Problem: A researcher's h-index is the largest h such that at least h papers have ≥ h citations each. python def hindex(c): c = sorted(c…
- Problem: A 3 × 4 matrix is sorted row-wise and each row starts after the previous row ends. Search it as one virtual sorted array of R·C…
- Problem: A truck holds at most 8 boxes. Each box type is (count, units per box). Maximise the total units loaded. python def…