Data Structures & AlgorithmsVery hard
In the 'first non-repeating character in a stream' problem, why is a queue with lazy removal used?
- A. Repeated characters at the front are removed only when they reach the front, and the new front is the first unique one.
- B. Each character is stored once and never removed.
- C. A queue sorts the characters alphabetically.
- 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.
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
- Why is a hash map (not a sorted array) used to find the longest run of consecutive integers in an unsorted array in linear expected time?
- When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership…
- A subarray's average is at least K exactly when which quantity is non-negative for that subarray?
- Convert the infix expression a+b(c-d)/e to postfix with the shunting-yard algorithm (operators '+', '-', '', '/' with '' and '/' binding…
- A 3-litre jug and a 5-litre jug start empty and you can fill either, empty either, or pour one into the other until one is full or empty…
- Reverse the sublist between positions 3 and 6 (1-based, inclusive) of the list 1, 2, 3, 4, 5, 6, 7, 8. What value is at position 4 of the…