Data Structures & AlgorithmsVery hard
When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership tests?
- A. A hash set of values seen so far, scanning the list once.
- B. Sorting the list and comparing adjacent values only.
- C. Recursively comparing each node with every node after it.
- D. A binary search tree of positions.
Correct answer: A. A hash set of values seen so far, scanning the list once.
A hash set answers 'seen before?' in expected O(1) while the list is traversed in its original order. Sorting would destroy the original order that the problem asks to keep.
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
- 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…
- Rotate the list 1, 2, 3, 4, 5, 6, 7 to the right by 10 positions (each rotation moves the last node to the front). What value is at the…
- Stable partition of the list [1, 4, 3, 2, 5, 2] around x = 4 puts every node with value < 4 first, then every node with value >= 4…