Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
Counting sort on [2, 0, 2, 1, 0, 2] (values 0–2) builds count[v] and then turns it into cumulative counts, count[v] = number of elements ≤ v. What is the cumulative count array?
- A. [2, 3, 6]
- B. [2, 1, 3]
- C. [0, 2, 3]
- D. [2, 3, 5]
Correct answer: A. [2, 3, 6]
Frequencies are 0: 2, 1: 1, 2: 3 → [2, 1, 3]. Running sums give [2, 3, 6]; the last entry always equals n = 6. Each cumulative value is one past the final position of that value, which is what makes the stable placement pass work. [0, 2, 3] is the exclusive (starting-index) version.
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
- Two pointers start at both ends of the sorted array [1, 3, 4, 6, 8, 11] to find pairs of DIFFERENT positions with sum 12 (move left if the…
- A hash map starts with capacity 16 and doubles its capacity whenever the number of keys EXCEEDS 0.75 × capacity. What is its capacity…
- Bubble sort (each pass compares adjacent pairs left to right and swaps if out of order) runs on [6, 2, 9, 1, 5]. What is the array after…
- The array [1, 2, 3, 4, 5, 6] is first rotated RIGHT by 2 positions and then rotated LEFT by 5 positions. What is the result?
- What is the longest common prefix of the strings interview, internet, interval and intern?
- The first k = 4 elements of the queue 1, 2, 3, 4, 5, 6 (front first) are reversed using a stack: dequeue 4 items onto the stack, pop them…