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?
  1. A. 3
  2. B. 1
  3. C. 2
  4. 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

Exams that ask this topic