Generate Every Permutation of Distinct Values

Quick Overview

Generate every permutation of distinct integers, with complete enumeration, restored search state, and factorial output-size accounting.

Generate Every Permutation of Distinct Values

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given the array `values` of distinct integers, return every permutation of its elements. Each returned array must contain every input element exactly once, arranged in a possible order. Return the complete collection in any order. The order inside each permutation is meaningful; the order in which the permutations are listed is not. ### Examples **Example 1** ```text Input: values = [2, -1, 4] Output: [[2, -1, 4], [2, 4, -1], [-1, 2, 4], [-1, 4, 2], [4, 2, -1], [4, -1, 2]] ``` All six arrangements are required. **Example 2** ```text Input: values = [0, 5] Output: [[5, 0], [0, 5]] ``` Reversing this collection's order would also be acceptable. ### Constraints & Assumptions - `1 <= values.length <= 6` - Every value is an integer between -10 and 10, inclusive. - All input values are distinct. - Every permutation uses each input occurrence exactly once. Return each permutation once. - No collection order or particular generation method is required. ### Clarifying Questions to Ask - Should a one-element input produce one result? Yes: the one-element array itself is its sole permutation. - Are negative numbers handled differently? No; their signs do not change which arrangements are possible. ```hint Account for available elements After a prefix has been chosen, every remaining input element must still be available for its own later position. ``` ### What a Strong Answer Covers - Complete enumeration without reusing or omitting an input element. - Restoration of temporary state between sibling choices. - A proof that every permutation appears exactly once. - Time and space costs that include the factorial number of returned arrays. ### Follow-up Questions - Could an implementation rearrange the input array in place while exploring choices? Explain how to restore it and how that changes temporary storage.

Overview: Generate every permutation of distinct integers, with complete enumeration, restored search state, and factorial output-size accounting.

Amazon logo
Amazon
Oct 16, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given the array values of distinct integers, return every permutation of its elements. Each returned array must contain every input element exactly once, arranged in a possible order.

Return the complete collection in any order. The order inside each permutation is meaningful; the order in which the permutations are listed is not.

Examples

Example 1

Input: values = [2, -1, 4]
Output: [[2, -1, 4], [2, 4, -1], [-1, 2, 4],
         [-1, 4, 2], [4, 2, -1], [4, -1, 2]]

All six arrangements are required.

Example 2

Input: values = [0, 5]
Output: [[5, 0], [0, 5]]

Reversing this collection's order would also be acceptable.

Constraints & Assumptions

  • 1 <= values.length <= 6
  • Every value is an integer between -10 and 10, inclusive.
  • All input values are distinct.
  • Every permutation uses each input occurrence exactly once. Return each permutation once.
  • No collection order or particular generation method is required.

Clarifying Questions to Ask Guidance

  • Should a one-element input produce one result? Yes: the one-element array itself is its sole permutation.
  • Are negative numbers handled differently? No; their signs do not change which arrangements are possible.

What a Strong Answer Covers Guidance

  • Complete enumeration without reusing or omitting an input element.
  • Restoration of temporary state between sibling choices.
  • A proof that every permutation appears exactly once.
  • Time and space costs that include the factorial number of returned arrays.

Follow-up Questions Guidance

  • Could an implementation rearrange the input array in place while exploring choices? Explain how to restore it and how that changes temporary storage.