Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: The array [6, 1, 9, 4, 4, 8] is scanned, and for each index the number of earlier elements that are strictly smaller is recorded. What is the sum of these counts?
- A. 9
- B. 8
- C. 6
- D. 4
Correct answer: B. 8
Counts per index are 0, 0, 2, 1, 1, 4, summing to 8. Using ≤ counts the equal 4 as well for 9, counting earlier larger elements gives 6, and counting the indices with a non-zero count gives 4.
More Coding Interview Problems questions
- Problem: The array [1, 5, 2, 6, 3, 7, 4, 8] is split into two halves, [1, 5, 2, 6] and [3, 7, 4, 8]. The result is formed by interleaving…
- Problem: How many integers from 1 to 1000 are perfect squares or perfect cubes (counted once even if both)?
- Problem: An undirected graph has edges 0-1, 1-2, 2-3, 3-4, 4-0, 1-5, 5-6, 6-2. What is the length of the shortest cycle that passes…
- Problem: In a directed graph with arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, the strongly connected components are formed. What is the size of the…
- Problem: The tree with edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8 is rooted at node 1. How many nodes have exactly two neighbours (degree 2)…
- Problem: How many occurrences of the substring "ana" are in "bananarama" (overlapping occurrences count)?