Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE

Problem: Pancake sort uses only prefix reversals on [3, 1, 2]. For each size from n down to 2, bring the largest value of the unsorted prefix to the front (one reversal, skipped if it is already at the front), then reverse the unsorted prefix so that value lands in its final place (one reversal, skipped if unnecessary). How many reversals are performed in total?

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Each placement needs at most two prefix reversals: one to bring the maximum to the front and one to move it to its final place. Reversing the whole array in one step is not a valid pancake move.

More Coding Interview Problems questions

Exams that ask this topic