Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: The array [4, 9, 2, 7, 5, 6] is processed by insertion sort (standard insertion into the sorted prefix, shifting larger elements right). How many element shifts (moves of an element one position to the right) occur in total?
- A. 11
- B. 6
- C. 15
- D. 7
Correct answer: D. 7
Shifts equal inversions: (4,2), (9,2), (9,7), (9,5), (9,6), (7,5), (7,6) = 7. Counting comparisons instead of shifts gives 11, missing one inversion gives 6, and 15 is the worst case C(6, 2).
More Coding Interview Problems questions
- Problem: The matrix [[1,2,3],[4,5,6],[7,8,9]] is traversed by rows, but with each row read alternately left-to-right and right-to-left…
- Problem: Find the number of pairs of indices (i, j), i < j, in the array [1, 3, 2, 4, 3, 5] such that a[i] = a[j] + 1 or a[i] = a[j] − 1.
- Problem: A grid has 3 rows and 4 columns. Starting at (0,0), a move goes right or down, and cells with odd row+column are forbidden. How…
- Problem: For the words ["bella", "label", "roller", "dinner", "ladder"], how many pairs (i, j) with i < j are anagrams of each other (same…
- Problem: The number 2024 is factorised. How many positive integers less than 10000 are divisors of 2024 × 2025?
- Problem: How many subarrays of [1, 2, 3, 1, 2, 3, 4] contain exactly 3 distinct values? Run the brute-force code. python a = [1, 2, 3, 1…