Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
What is the minimum number of swaps of two arbitrary elements needed to sort the array [3, 1, 4, 2] into increasing order?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: C. 3
Map each element to its sorted position; the permutation splits into cycles, and a cycle of length L needs L - 1 swaps. Counting inversions gives a different quantity, which is a trap for swap counts.
More Coding Interview Problems questions
- Given the numbers 3, 30, 34 and 5, arrange them in some order and concatenate the decimal strings. What is the largest number that can be…
- Problem: Count (possibly overlapping) occurrences of a pattern in a text with KMP. python def kmpcount(t, p): f = [0] len(p); k = 0 for i…
- Problem: Count DISTINCT palindromic substrings by expanding around centres and collecting them in a set. python def distinctpal(s): seen =…
- Problem: Build the suffix array of a string (start indices of suffixes in sorted order) and its LCP array (Kasai's algorithm). python def…
- Problem: A Fenwick tree over a DIFFERENCE array supports 'add v to a[l..r]' and 'read a[i]' in O(log n) (1-indexed, all zeros initially)…
- Problem: Point-update / range-maximum segment tree (iterative, size n = 8). python n = 8; t = [0] (2 n) def upd(i, v): i += n; t[i] = v…