Code Room
CodingMediumcod-g204
Subject Sorting variantsLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You may sort an array only by 'pancake flips': choosing an index k and reversing the prefix arr[0..k-1] (the first k elements). Return any sequence of flip sizes k (each 1 <= k <= len(arr)) that, applied in order, sorts the array ascending. The given reference produces one valid canonical sequence by repeatedly bringing the current maximum to its final position. The array may contain duplicates and has length >= 1; return an empty list if it is already sorted by this procedure.

Implement
pancake_sort(arr: list[int]) → list[int]
Examples
in[[3,2,4,1]]out[3,4,2,3,2]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.