FastPrepMinimum Swaps to Sort an Array
Problems/Minimum Swaps to Sort an Array

Minimum Swaps to Sort an Array

Postman logoPostman● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Postman online assessment. The report names the task of sorting with the fewest swaps, but it does not say whether to sort up or down, or how to handle repeated values. This version sorts in ascending order and uses distinct values. Our version is about 94% the same as the reported question.

Given an array values of distinct integers, return the minimum number of swaps of any two positions needed to arrange the array in strictly increasing order.

Function

minimumSwaps(values: int[]) → int

Examples

Example 1

values = [4,3,1,2]return = 3

One optimal sequence swaps the values at positions 0 and 2, then positions 1 and 3, then positions 2 and 3, producing [1,2,3,4].

Example 2

values = [1,5,4,3,2]return = 2

Swap 5 with 2, then swap 4 with 3.

Constraints

  • 1 ≤ values.length ≤ 2 × 10^5
  • -10^9 ≤ values[i] ≤ 10^9
  • All values are distinct.

More Postman interview questions

  • Configuration SystemOA · Seen Sep 2020
  • Large ResponsesOA · Seen Sep 2020
  • Validate IP AddressOA · Seen Aug 2020
  • Without WhitespacesOA · Seen Sep 2019
  • Maximum Laptop Rating in a Price RangeOA · Seen Aug 2019
  • Group Duplicate Files by ContentONSITE INTERVIEW · Seen Feb 2026
  • Encode and Decode a String StreamONSITE INTERVIEW · Seen May 2024

Similar questions at other companies

  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
  • Deterministic Depth-First Graph TraversalWaymo · PHONE SCREENWaymo interview questions
  • Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
  • Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
  • Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
All Postman interview questionsSee Postman hiring insights
Saved
class Solution {
    public int minimumSwaps(int[] values) {
        // write your code here
    }
}
Checking account…