Data Structures & AlgorithmsVery hard

A stream arrives one character at a time: a a b c b d. After each character, report the FIRST character (in arrival order) that has appeared exactly once so far, or # if none. Give the reports as a space-separated string of six symbols.

  1. A. a # b c c d
  2. B. a # b b c c
  3. C. a # b b c d
  4. D. a # a a a a

Correct answer: B. a # b b c c

Keep the characters in a queue and remove the front while it is repeated; the front is then the first non-repeating character. Reporting the last unique character, or reusing a stale answer, produces a different string.

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

Exams that ask this topic