Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT
A linked list holds the values [1, 2, 3, 4, 5, 6, 7, 8]. Reverse every complete group of 3 consecutive nodes; a final group with fewer than 3 nodes is left as it is. Which list of values results?
- A. [4, 3, 2, 1, 8, 7, 6, 5]
- B. [8, 7, 6, 5, 4, 3, 2, 1]
- C. [3, 2, 1, 6, 5, 4]
- D. [3, 2, 1, 6, 5, 4, 8, 7]
Correct answer: D. [3, 2, 1, 6, 5, 4, 8, 7]
Reverse each block of k independently, keeping the short tail unchanged. Reversing the whole list, or using a block size that does not match the request, gives a different order.
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
- You may delete one contiguous block from [4, -9, 3, 5, -2, 7, -8, 6] (possibly empty block) and keep the prefix before it and the suffix…
- What is the length of the longest common prefix of the two strings obtained by taking all cyclic rotations of "banana" and keeping only…
- Count the NON-EMPTY subsets of the 12 items 3 5 6 7 2 9 4 8 1 10 11 12 (chosen by position) whose sum is divisible by 4.
- How many NON-EMPTY subsequences of 1 2 3 5 6 8 9 (subsets chosen by position) have (maximum − minimum) ≤ 2?
- How many NON-EMPTY subsequences of 2 3 4 5 6 8 (position subsets) have a product congruent to 1 modulo 7?
- How many subsequences of EXACTLY three elements of 1 2 3 4 5 6 7 8 (chosen by position) have an EVEN sum?