Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed?
Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed?
from collections import Counter
for s in ['ccdbaad', 'bababbd', 'bdbcdac']:
c = Counter(s)
print(next((i for i, ch in enumerate(s) if c[ch] == 1), -1), end=" ")- A. 3 6 5
- B. 3 7 5
- C. 0 1 0
- D. 3 6 6
Correct answer: A. 3 6 5
Two passes: count every character, then return the first position whose count is 1. Output: 3 6 5.
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
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [−4, 5, 8, −4, −4…
- A hash table with open addressing has load factor α = 0.75 and uniform hashing. What is the expected number of probes for an UNSUCCESSFUL…
- Insertion sort is run on an array of n elements that has exactly k inversions. What is its running time?
- A queue is built from two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving every element of IN onto OUT only when OUT…
- Rearrange a linked list so that nodes at odd POSITIONS (1st, 3rd, …) come first, followed by the even-position nodes, keeping relative…
- Sort [−4, 3, −3, −5, −4, 5, 2] and, for each i, move two pointers to find the triplet sum closest to -2. What is that closest sum?