Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
HomeLearnDSAInterview Coding PatternsCore Patterns
✓ FreeIntermediate· 10 min read

Cyclic Sort

Place numbers in their correct positions in-place — find missing/duplicate numbers in O(n).

Published September 21, 2026


Cyclic Sort

Cyclic sort is a special sorting technique for arrays where elements are in a known range [1, n]. It sorts in O(n) time and O(1) space by placing each number at its correct index.

The Pattern

For an array with values 1 to n, the correct position for value v is index v-1. Cycle through the array, swapping each element to its correct position.

void cyclicSort(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int correctIdx = nums[i] - 1; // where nums[i] should be
        if (nums[i] != nums[correctIdx]) {
            // Swap nums[i] to its correct position
            int tmp = nums[i];
            nums[i] = nums[correctIdx];
            nums[correctIdx] = tmp;
        } else {
            i++; // nums[i] is at its correct position
        }
    }
}
// [3,1,5,4,2] → sort → [1,2,3,4,5]

Find Missing Number

public int missingNumber(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i];
        if (j < nums.length && nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    // Find the first position where nums[i] != i
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k) return k;
    return nums.length; // missing number is n
}
// [3,0,1] → sort → [0,1,3] → position 2 is wrong → missing = 2

Find All Missing Numbers

public List<Integer> findDisappearedNumbers(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i] - 1;
        if (nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    List<Integer> missing = new ArrayList<>();
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k + 1) missing.add(k + 1);
    return missing;
}

Find Duplicate Number

public int findDuplicate(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        if (nums[i] != i + 1) {
            int j = nums[i] - 1;
            if (nums[i] != nums[j]) {
                int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
            } else {
                return nums[i]; // duplicate found!
            }
        } else {
            i++;
        }
    }
    return -1;
}

Find All Duplicates

public List<Integer> findAllDuplicates(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i] - 1;
        if (nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    List<Integer> duplicates = new ArrayList<>();
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k + 1) duplicates.add(nums[k]);
    return duplicates;
}

When to Apply Cyclic Sort

✅ Array contains numbers in range [1, n] or [0, n-1] ✅ Problem asks for missing/duplicate/corrupted numbers ✅ O(1) space required

Interview Tips

  1. Cyclic sort is O(n) time — each element is moved at most once to its correct position, so total swaps ≤ n.
  2. The key check: nums[i] != nums[j] before swapping prevents infinite loops with duplicates.
  3. After sorting, scan for nums[i] != i+1 to identify missing or duplicate values.

Previous

Merge Intervals

Next

Top-K Elements

AI Tutor

Lesson: Cyclic Sort

Quick actions

AI responses can be inaccurate. Verify critical information.