Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
What is printed? (frequency of the most common character, ties to the earliest)
What is printed? (frequency of the most common character, ties to the earliest)
s = "programming" best = max(s, key=s.count) print(best, s.count(best), len(set(s)))
- A. r 2 8
- B. g 2 8
- C. m 2 8
- D. r 2 11
Correct answer: A. r 2 8
r, g and m each appear twice; max returns the FIRST element with the maximum key, which is 'r' (index 1). The string has 8 distinct letters (p, r, o, g, a, m, i, n). Note s.count inside max makes this O(n²); a Counter is O(n).
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
- Sliding window: what is printed? (maximum sum of any 3 consecutive elements) c int a[] = {4, -1, 6, -2, 3, 5, -4, 2}; int n = 8, k = 3, s…
- What is printed? (two-pointer: remove all occurrences of a value in place) cpp vector<int> a = {3, 2, 2, 3, 4, 3, 5}; int w = 0; for (int…
- What is printed? (stack: check if a sequence of pops is possible after pushing 1..n in order) python def valid(pushed, popped): st, j =…
- What is printed? (next greater element to the right, −1 if none) python a = [4, 5, 2, 25, 7, 8] res, st = [-1] len(a), [] for i in…
- Hash map counting: what is printed? (first element that repeats, scanning left to right) python def firstrepeat(a): seen = set() for x in…
- What is printed? (anagram check two ways) python from collections import Counter x, y = "listen", "enlists" print(sorted(x) == sorted(y)…