Data Structures & AlgorithmsVery hard

In the 'first non-repeating character in a stream' problem, why is a queue with lazy removal used?

  1. A. Repeated characters at the front are removed only when they reach the front, and the new front is the first unique one.
  2. B. Each character is stored once and never removed.
  3. C. A queue sorts the characters alphabetically.
  4. D. The answer is always the last character received.

Correct answer: A. Repeated characters at the front are removed only when they reach the front, and the new front is the first unique one.

Characters are appended in arrival order; a character that has become repeated is dropped only when it is at the front. Each character enters and leaves the queue once, so the stream is processed in linear time.

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

Exams that ask this topic