Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The array [4, 1, 3, 2, 5] is processed by counting sort on the values 1 to 5, writing the values into an output array in sorted order. How many positions of the output array hold a value that equals its own index plus one (1-based position equals value)?
- A. 6
- B. 4
- C. 7
- D. 5
Correct answer: D. 5
After sorting, the output is 1 through 5 in order, so every position satisfies position equals value. The count is the array length, which is a quick sanity check for counting sort.
More Coding Interview Problems questions
- Problem: For the set {2, 3, 4, 6, 8, 9, 12}, how many pairs (x, y) with x < y have x dividing y?
- Problem: A bag contains 2 red, 3 green and 4 blue balls. How many ways are there to choose 3 balls so that all three have different colours?
- Problem: The string "abcdefghij" is read two letters at a time in pairs; each pair (xy) is replaced by the pair reversed (yx). What is the…
- Problem: The sum of the numbers in the sequence 1, 1+2, 1+2+3, …, up to 1+2+…+12 is what value?
- Problem: In the array [2, 4, 6, 8, 10], count the number of subarrays whose sum is divisible by 4.
- Problem: A stack-based bracket checker reads the string "([]{()})[" and reports the number of unmatched opening brackets at the end (0 if…