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=" ")
  1. A. 3 6 5
  2. B. 3 7 5
  3. C. 0 1 0
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic