Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: What is the value printed by this snippet? from collections import Counter print(Counter("abracadabra").most_common(2))
- A. [('a', 5), ('r', 2)]
- B. [('a', 5), ('c', 1)]
- C. [('a', 5), ('b', 2)]
- D. [('b', 2), ('a', 5)]
Correct answer: C. [('a', 5), ('b', 2)]
most_common returns counts in descending order, and equal counts keep their first-seen order. Counting letters manually and reading them in alphabetical order gives a different pair.
More Coding Interview Problems questions
- Problem: Consider the list a = [2, 4, 6, 8]. The loop 'for x in a: if x % 2 == 0: a.remove(x)' is run. What is the list a afterwards?
- Problem: How many recursive calls are made in total when naive Fibonacci fib(n) = fib(n−1) + fib(n−2) with fib(0) = fib(1) = base cases…
- Problem: What is printed? print(f"{3.14159:.2f}", f"{42:05d}", f"{0.5:.0%}")
- Problem: In Python, what is the value of (−5 >> 1) + 10, where >> is the arithmetic right shift applied to the negative integer −5?
- Problem: Records (name, score) are sorted by score descending with Python's stable sort…
- Problem: A deque holds 1, 2, 3, 4, 5 from left to right. The operation rotate(2) moves the last 2 elements to the front (rotate right by…