interview questions shared by candidates
Suppose you have a stack of pancakes of pairwise distinct sizes. You wish to order the pancakes the pancakes by size, with the largest one on the bottom. The only operation is available to you is a spatula flip: You can insert a spatula anywhere in the pancake stack and flip over the stack above the spatula, reversing the order of the pancakes. For instance, in the following stack bottom . 2 3 | 5 1 4 . top Inserting the spatula between 3 and 5 will give: bottom 2 3 4 1 5 | top Design an algorithm to sort the pancake stack using this operation. Write a program to execute this algorithm in C++. What is the runtime efficiency (precisely, not just in Big-O time), assuming that a flip is an atomic operation? Is your algorithm optimal (again, precisely, not just asymptotically)?