FastPrepMinimum Operations to Sort with Rotations Then Swaps
Problems/Minimum Operations to Sort with Rotations Then Swaps

Minimum Operations to Sort with Rotations Then Swaps

Bank of New York Mellon logoBank of New York Mellon● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Bank of New York Mellon (BNY) included this sorting puzzle in a five-part online assessment for 2027 new-grad candidates. The array is a shuffled 1-to-n sequence. One kind of move cycles the whole array, another exchanges any pair, and cycling is only possible before the first exchange. The report stated these rules and an upper size of 1000.

The report gave no function signature, no sample arrays and no cost model. Our choices here: each rotation and each swap costs 1, and the answer is the smallest total count.

Our version is about 95% the same as the reported question.

You are given permutation, an array containing each integer from 1 to n exactly once. You want to sort it into ascending order using two kinds of operations, each costing 1:

  • Rotate left: move the first element to the end of the array. For example, [3, 4, 1, 2] becomes [4, 1, 2, 3].
  • Swap: exchange any two elements.

All rotations must happen before the first swap: once you swap, you can no longer rotate. Return the minimum total number of operations needed to sort the array.

Function

minOperations(permutation: int[]) → int

Examples

Example 1

permutation = [3,4,1,2]return = 2
  • Two left rotations turn [3, 4, 1, 2] into [4, 1, 2, 3] and then [1, 2, 3, 4], which is sorted: 2 operations.
  • Without rotating, the array needs 2 swaps as well (swap 3 with 1, then 4 with 2), so the answer is 2.

Example 2

permutation = [2,3,4,5,1]return = 4
  • Rotating would take 4 left rotations to bring 1 to the front.
  • Instead, swap without rotating: the values form one cycle of length 5, which needs 4 swaps, so the answer is 4.

Example 3

permutation = [5,1,2,3,4]return = 1
  • One left rotation gives [1, 2, 3, 4, 5], so the answer is 1.

Constraints

  • 1 <= n == permutation.length <= 1000
  • permutation contains each integer from 1 to n exactly once.

More Bank of New York Mellon Online Assessment questions

  • Count SubsequencesOA · Seen Aug 2024
  • Digit SumOA · Seen Aug 2024

Similar questions at other companies

  • Optimal TransferSnowflake · OASnowflake interview questions
  • Maximum XOR Elimination ScoreRippling · OARippling interview questions
  • Find the Celebrity from Pairwise AcquaintancesAmazon · ONSITE INTERVIEW
  • Find the Celebrity from Pairwise AcquaintancesSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
  • Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
  • Maximum ScoreNVIDIA · OANVIDIA interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA

Interview reports that mention this question

  • Bank of New York Mellon New Grad OA: 1 Experience
See Bank of New York Mellon hiring insights
Saved
class Solution {
    public int minOperations(int[] permutation) {
        // Write your code here.
    }
}
Checking account…