Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the minimum number of moves to sort [4, 1, 3, 2] when one move takes any element to the front? Run the code (answer = n - LIS).
Problem: What is the minimum number of moves to sort [4, 1, 3, 2] when one move takes any element to the front? Run the code (answer = n - LIS).
a = [4, 1, 3, 2]
import bisect
tails = []
for x in a:
i = bisect.bisect_left(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
print(len(a) - len(tails))What is printed?- A. 3
- B. 1
- C. 2
- D. 4
Correct answer: C. 2
Elements in a longest increasing subsequence never need to move, so the answer is n minus the LIS length.
More Coding Interview Problems questions
- Problem: What is the running time of Ford-Fulkerson when augmenting paths are found by DFS with integer capacities?
- Problem: Which algorithm finds the maximum subarray sum in O(n log n) by splitting the array and handling the crossing case?
- Problem: What does the Pisano period of m describe?
- Problem: Which characterisation of bipartite graphs is correct?
- Problem: What does the Erdos-Szekeres theorem state?
- Problem: What does Dilworth's theorem say about a finite partially ordered set?