Data Structures & AlgorithmsModerateFlipkart/Uber OA · placement
Group words that are anagrams of each other using the sorted word as the key. What is printed?
Group words that are anagrams of each other using the sorted word as the key. What is printed?
from collections import defaultdict
g = defaultdict(list)
for w in ['god', 'enlist', 'listen', 'cat', 'silent', 'dog', 'tinsel', 'google']:
g["".join(sorted(w))].append(w)
print(len(g), max(len(v) for v in g.values()))- A. 4 3
- B. 6 4
- C. 4 4
- D. 2 5
Correct answer: C. 4 4
Anagrams share the same sorted letters, so they land in one bucket. Output (groups, largest group): 4 4.
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
- What does this loop-counting code print? (Python) python n, c = 37, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [14, 8, 20, 57, 38, 4…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 4 positions. What is the final array?
- What does this loop-counting code print? (Python) python n, c = 10, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…