FastPrepSort by Checking Permutations

Sort by Checking Permutations

Mercor logoMercor● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a deliberately inefficient sorting method: enumerate permutations of values and test whether each permutation is in nondecreasing order. Return a sorted permutation.

Every occurrence must be preserved, including duplicate values. The input array may be empty; its sorted permutation is also empty.

The result is the values in ascending order. The exercise is intended to practice permutation generation and sortedness checks, rather than using a library sort.

Function

permutationSort(values: int[]) → int[]

Examples

Example 1

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

The permutation [1,2,3] is nondecreasing and preserves every input value.

Example 2

values = [2,2,-1]return = [-1,2,2]

Both occurrences of 2 remain in the result.

Example 3

values = []return = []

The empty permutation is already sorted.

Constraints

  • 0 <= values.length <= 8.
  • -10^9 <= values[i] <= 10^9.

More Mercor problems

See Mercor hiring insights
public int[] permutationSort(int[] values) {
    // Write your code here.
}
values[3,1,2]
expected[1,2,3]
Checking account…