PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array manipulation and permutation-based reasoning for minimizing swap operations, including handling of duplicate values and performance considerations, within the Coding & Algorithms domain for a Data Scientist role.

  • hard
  • Akuna Capital
  • Coding & Algorithms
  • Data Scientist

Find minimum swaps to sort array with duplicates

Company: Akuna Capital

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

### Problem Given an integer array `nums` of length `n` that may contain duplicate values, you may perform swaps where you choose **any two indices** `i` and `j` and swap `nums[i]` and `nums[j]`. Return the **minimum number of swaps** required to reorder `nums` into **non-decreasing** order. ### Input - An integer array `nums`. ### Output - An integer: the minimum number of swaps needed to make `nums` sorted in non-decreasing order. ### Constraints - `1 <= n <= 2 * 10^5` - `-10^9 <= nums[i] <= 10^9` ### Notes - Because duplicates exist, there can be multiple valid sorted targets; your answer should be the minimum swaps over all valid ways to reach a sorted array. ### Examples 1. `nums = [2, 1, 2]` → sorted form is `[1, 2, 2]`, answer `1` (swap indices 0 and 1). 2. `nums = [1, 5, 1, 5]` → answer `1` (swap the two middle elements to get `[1,1,5,5]`).

Quick Answer: This question evaluates array manipulation and permutation-based reasoning for minimizing swap operations, including handling of duplicate values and performance considerations, within the Coding & Algorithms domain for a Data Scientist role.

Given an integer array `nums` of length `n` that may contain duplicate values, you may perform a swap by choosing **any two indices** `i` and `j` and exchanging `nums[i]` and `nums[j]`. Return the **minimum number of swaps** required to reorder `nums` into **non-decreasing** order. Because duplicates may exist, there can be multiple valid sorted arrangements (equal elements are interchangeable). Your answer must be the minimum number of swaps over **all** valid ways to reach a non-decreasing arrangement. **Examples** - `nums = [2, 1, 2]` -> `1` (swap indices 0 and 1 to get `[1, 2, 2]`). - `nums = [1, 5, 1, 5]` -> `1` (swap the two middle elements to get `[1, 1, 5, 5]`). **Constraints** - `1 <= n <= 2 * 10^5` - `-10^9 <= nums[i] <= 10^9`

Constraints

  • 1 <= n <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9
  • A swap may exchange any two indices (not just adjacent ones).
  • Equal values are interchangeable; answer is the minimum over all valid sorted targets.

Examples

Input: [2, 1, 2]

Expected Output: 1

Explanation: Swap indices 0 and 1 -> [1, 2, 2]. One swap.

Input: [1, 5, 1, 5]

Expected Output: 1

Explanation: Swap the two middle elements -> [1, 1, 5, 5]. One swap.

Input: [1, 2, 3]

Expected Output: 0

Explanation: Already non-decreasing; no swaps needed.

Input: [3, 2, 1]

Expected Output: 1

Explanation: Swap indices 0 and 2 -> [1, 2, 3]. The 2 is already in place, forming a single 2-cycle.

Input: [5]

Expected Output: 0

Explanation: Single element is trivially sorted.

Input: [2, 2, 2, 2]

Expected Output: 0

Explanation: All equal; every arrangement is already non-decreasing.

Input: [4, 3, 2, 1]

Expected Output: 2

Explanation: Two disjoint swaps: (0,3) and (1,2) -> [1, 2, 3, 4].

Input: [2, 1, 3, 1, 2]

Expected Output: 2

Explanation: Duplicates let you pick targets so the work splits into two cycles -> sorts in 2 swaps.

Input: [-1, -1, 0, -2]

Expected Output: 2

Explanation: Negatives and a duplicate -1; minimum is 2 swaps to reach [-2, -1, -1, 0].

Input: [2, 4, 3, 6, 5, 1, 3, 2, 1]

Expected Output: 6

Explanation: A tangled case where greedy shortest-cycle-first would report 7; the exact max-cycle decomposition yields 3 cycles over 9 misplaced elements, so 6 swaps.

Hints

  1. Sort a copy of nums to get the target. Map this to a permutation problem: minimum swaps to realize a permutation is (#elements that move) - (#cycles).
  2. With duplicates you may choose which equal element goes to which equal slot. To minimize swaps you want to MAXIMIZE the number of cycles, so model it on VALUES: add an edge value_here -> value_that_belongs_here for every out-of-place position.
  3. Every 2-cycle (a pair of values pointing at each other) is the shortest possible cycle and can always be taken first. After removing all 2-cycles, decompose each remaining component into as many cycles as possible. Beware: greedy shortest-cycle-first is NOT always optimal -- e.g. [2,4,3,6,5,1,3,2,1] needs an exact decomposition (answer 6).
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Graph Spread and Portfolio Trades - Akuna Capital (medium)
  • Compute max profit across dated stock quotes - Akuna Capital (Medium)
  • Break a palindrome to smallest non-palindrome - Akuna Capital (Medium)
  • Count inversions in an array efficiently - Akuna Capital (Medium)
  • Compute min operations to transform integers - Akuna Capital (Medium)