Sort a Permutation Using a Bounded Sequence of Prefix Reversals

Quick Overview

Sort a Permutation Using a Bounded Sequence of Prefix Reversals. A pancake flip chooses 1 <= k <= arr.length and reverses the first k values.

Sort a Permutation Using a Bounded Sequence of Prefix Reversals

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A pancake flip chooses `1 <= k <= arr.length` and reverses the first k values. Given a permutation of1 through n, return any sequence of flip lengths that sorts it in ascending order using at most10*n flips. A minimum-length sequence is not required. ```cpp class Solution { public: vector<int> pancakeSort(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [2,3,1] Output: [2,3] Explanation: Flip2 gives[3,2,1], then flip3 gives[1,2,3]. ``` **Example 2:** ```text Input: arr = [2,1] Output: [2] Explanation: Reversing the full prefix sorts both values in one flip. ``` ### Constraints - `1 <= arr.length <= 100`. - Every value lies between1 and arr.length. - Values are unique, so arr is a permutation of1 through arr.length. - The returned sequence must use at most10*arr.length flips. ### Contract considerations The source allows any sorting flip sequence under the stated bound. It does not require the fewest flips. ```hint Read the boundary condition Place the largest unfinished value at the end of its prefix, then shrink the unfinished region. ``` ### What a Strong Answer Covers The source allows any sorting flip sequence under the stated bound. It does not require the fewest flips. ### Follow-up Questions Optional practice extension, separate from the original task: How could you validate the flips without requiring one particular reference sequence?

Overview: Sort a Permutation Using a Bounded Sequence of Prefix Reversals. A pancake flip chooses 1 <= k <= arr.length and reverses the first k values.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A pancake flip chooses 1 <= k <= arr.length and reverses the first k values. Given a permutation of1 through n, return any sequence of flip lengths that sorts it in ascending order using at most10*n flips. A minimum-length sequence is not required.

class Solution {
public:
    vector<int> pancakeSort(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [2,3,1]
Output: [2,3]
Explanation: Flip2 gives[3,2,1], then flip3 gives[1,2,3].

Example 2:

Input: arr = [2,1]
Output: [2]
Explanation: Reversing the full prefix sorts both values in one flip.

Constraints

  • 1 <= arr.length <= 100 .
  • Every value lies between1 and arr.length.
  • Values are unique, so arr is a permutation of1 through arr.length.
  • The returned sequence must use at most10*arr.length flips.

Contract considerations

The source allows any sorting flip sequence under the stated bound. It does not require the fewest flips.

What a Strong Answer Covers Guidance

The source allows any sorting flip sequence under the stated bound. It does not require the fewest flips.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How could you validate the flips without requiring one particular reference sequence?