Data Structures & AlgorithmsModerateTCS NQT · Arrays
The sorted array [1, 1, 2, 2, 2, 3, 5, 5, 8] is de-duplicated in place by two pointers: a write index w starts at 1 and each element a[i] (i from 1) that differs from a[w-1] is copied to a[w] and w is incremented. What is the final value of w?
- A. 4
- B. 6
- C. 7
- D. 5
Correct answer: D. 5
Distinct values are 1, 2, 3, 5, 8, so five slots are filled and w = 5.
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
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated left by 3 positions using three reversals: reverse the first 3 elements, reverse the…
- Keys 12, 22, 32, 13, 23 are inserted in this order into an empty hash table of size 10 using h(k) = k mod 10 and linear probing. At which…
- A queue is built from two stacks: enqueue pushes on stack IN; dequeue pops from OUT, first moving all of IN onto OUT (one pop and one push…
- What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] in ascending order?
- For each element of [4, 5, 2, 25, 7, 8], take the first strictly greater element to its right (or -1 if there is none). What is the sum of…
- A sliding window of size 3 moves over [1, 3, -1, -3, 5, 3, 6, 7]. What is the sum of the window maximums?