Data Structures & AlgorithmsModerate
A sorted linked list holds 1 2 2 3 3 3 4 4 4 4. Delete nodes so that each value keeps AT MOST TWO nodes, preserving order. How many nodes remain?
- A. 4
- B. 6
- C. 7
- D. 9
Correct answer: C. 7
Walk the list once, keeping a node when it differs from the node two places before it in the kept list. Counting distinct values or subtracting the largest group ignores the keep-two rule.
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
- How many different valid sequences of PUSH and POP operations are there for a stack that performs exactly 6 pushes and 6 pops in total…
- How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?
- What is the maximum sum of a contiguous subarray of 4 2 4 5 6 2 8 that contains NO repeated value?
- How many contiguous subarrays of length at least 2 of 1 1 2 2 2 3 1 1 1 1 consist of ONE repeated value?
- Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?
- How many index pairs i < j in 1 4 1 4 4 2 1 2 2 hold EQUAL values and satisfy j − i ≤ 2?