The Worst Possible Sorting Algorithm and How to Enumerate Every Permutation

Read the full interview experience this question came from →

Quick Overview

A verbal algorithms question: name the worst sorting algorithm that still always sorts correctly, analyze its running time, and explain how you would compute every permutation of the input exactly once. It tests reasoning about factorial complexity and knowledge of permutation enumeration methods.

The Worst Possible Sorting Algorithm and How to Enumerate Every Permutation

Company: Mercor

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

This is a verbal question from a technical screen with no coding. The interviewer asks for the worst sorting algorithm you can think of and its running time, and then asks how you would compute each permutation of the input. ### Clarifying Questions - Does "worst" mean the slowest algorithm that still always terminates with a correct result, or may it include deliberately wasted work? - Should the running time be the worst case, the average case, or both? - May the input contain repeated values? ### Part 1 — The worst sorting algorithm What is the worst sorting algorithm you can think of that still always returns a correctly sorted result? What is its running time, and why? ```hint Know nothing, guess everything Imagine the only tool you are allowed to use is a check that tells you whether a given arrangement is sorted. How would you find the sorted arrangement, and how many candidates might you have to try? ``` #### What This Part Should Cover - A correct algorithm and an argument that it always terminates - Its worst-case running time, derived by counting candidates and the cost of each - A randomized variant and how its guarantees differ - What "worst" has to mean for the question to have an answer ### Part 2 — Computing each permutation The interviewer follows up: explain how you would compute each permutation of n elements, so that every arrangement is produced exactly once. ```hint List them in an order Think of an order in which all arrangements can be listed, and how to get from one arrangement to the next without storing the ones already produced. ``` #### What This Part Should Cover - At least one method that produces every permutation exactly once, with an argument why - Its time per permutation, its total time and its extra space - The behavior with repeated values - How the generator plugs into the sort, including stopping early ### What a Strong Answer Covers - A correct factorial-time algorithm with an honest running-time analysis that includes the cost of building and checking each candidate - A clear definition of "worst" that excludes padding with idle work - A concrete, correct permutation-generation method, explained step by step - Complexity of generation and awareness of alternatives with different trade-offs - Edge cases: empty input, a single element, repeated values, and how quickly the factorial grows ### Follow-up Questions - A random-shuffle variant keeps shuffling until the array is sorted. What is its expected running time, and is it guaranteed to terminate? - How would you generate a uniformly random permutation, and what goes wrong with a shuffle that swaps each position with any position? - How would you jump directly to the k-th permutation in lexicographic order without generating the earlier ones? - Can a sorting algorithm be made arbitrarily slow, and what rule would make "worst" a meaningful question?

Overview: A verbal algorithms question: name the worst sorting algorithm that still always sorts correctly, analyze its running time, and explain how you would compute every permutation of the input exactly once. It tests reasoning about factorial complexity and knowledge of permutation enumeration methods.

Read the full Mercor Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/Mercor
Mercor logo
Mercor
Oct 10, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

This is a verbal question from a technical screen with no coding. The interviewer asks for the worst sorting algorithm you can think of and its running time, and then asks how you would compute each permutation of the input.

Clarifying Questions Guidance

  • Does "worst" mean the slowest algorithm that still always terminates with a correct result, or may it include deliberately wasted work?
  • Should the running time be the worst case, the average case, or both?
  • May the input contain repeated values?

Part 1 — The worst sorting algorithm

What is the worst sorting algorithm you can think of that still always returns a correctly sorted result? What is its running time, and why?

What This Part Should Cover Guidance

  • A correct algorithm and an argument that it always terminates
  • Its worst-case running time, derived by counting candidates and the cost of each
  • A randomized variant and how its guarantees differ
  • What "worst" has to mean for the question to have an answer

Part 2 — Computing each permutation

The interviewer follows up: explain how you would compute each permutation of n elements, so that every arrangement is produced exactly once.

What This Part Should Cover Guidance

  • At least one method that produces every permutation exactly once, with an argument why
  • Its time per permutation, its total time and its extra space
  • The behavior with repeated values
  • How the generator plugs into the sort, including stopping early

What a Strong Answer Covers Guidance

  • A correct factorial-time algorithm with an honest running-time analysis that includes the cost of building and checking each candidate
  • A clear definition of "worst" that excludes padding with idle work
  • A concrete, correct permutation-generation method, explained step by step
  • Complexity of generation and awareness of alternatives with different trade-offs
  • Edge cases: empty input, a single element, repeated values, and how quickly the factorial grows

Follow-up Questions Guidance

  • A random-shuffle variant keeps shuffling until the array is sorted. What is its expected running time, and is it guaranteed to terminate?
  • How would you generate a uniformly random permutation, and what goes wrong with a shuffle that swaps each position with any position?
  • How would you jump directly to the k-th permutation in lexicographic order without generating the earlier ones?
  • Can a sorting algorithm be made arbitrarily slow, and what rule would make "worst" a meaningful question?
Loading comments...