FastPrepMinimum Swaps
Problems/Minimum Swaps

Minimum Swaps

Akuna Capital logoAkuna Capital● MediumINTERNOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The judged task matches the visible source at about 99%.

You are given an array popularity containing the unique popularity ratings of n items.

The shopkeeper wants the items arranged from left to right in decreasing popularity.

  • In one operation, the shopkeeper can swap any two items.
  • Your task is to determine the minimum number of swaps needed to achieve the correct decreasing order.

Function

minimumSwaps(popularity: int[]) → int

Examples

Example 1

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

Suppose there are n = 4 items, and popularity = [3, 4, 1, 2].

Output: 2

Explanation

  1. First swap: Switch items with ratings 3 and 4 to get [4, 3, 1, 2].
  2. Second swap: Switch items with ratings 1 and 2 to get [4, 3, 2, 1].

Constraints

  • 1 ≤ n ≤ 2 × 10^5
  • 1 ≤ popularity[i] ≤ n
  • Test Case Input Format
    • The first line contains the integer n.
    • The next n lines contain an integer element of popularity[i].

More Akuna Capital Online Assessment questions

  • Last Digit of the Ways to Split Traders Between Two OfficesOA · Seen Dec 2026
  • Light Bulbs Left On After All Engineers FinishOA · Seen Dec 2026
  • Delivery Management SystemOA · Seen Nov 2026
  • Count Server ReplacementsOA · Seen Nov 2026
  • Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximum Remaining Drone PowerOA · Seen Nov 2026
  • Rolling MetricsOA · Seen Oct 2026
  • Vowel Substring GameOA · Seen Sep 2026
  • Competitive ForagingOA · Seen Sep 2026
  • Binary CircuitOA · Seen Sep 2026

Similar questions at other companies

  • API Rate Limit Violation DetectionTekion · OATekion interview questions
  • Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
  • Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA

Akuna Capital interview experiences

  • Akuna Capital Quant Trader and QR Interviews: 5 Experiences
  • Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
  • Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
  • Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
  • Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
  • Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)
All Akuna Capital interview questionsSee Akuna Capital hiring insights
Saved
class Solution {
    public int minimumSwaps(int[] popularity) {
      // write your code here
    }
}
Checking account…