Scale customer reach and grow sales with AskHandle chatbot

How to Build an Array from a Permutation in Coding Interviews?

When preparing for a tech interview, you might encounter questions involving arrays and permutations. One common problem asks you to build an array based on a given permutation. This task is straightforward, but understanding the logic behind it is essential. In this article, I'll explain what this problem is about, how to approach it, and provide a clear example with code.

image-1
Written by
Published onJuly 16, 2025
RSS Feed for BlogRSS Blog

How to Build an Array from a Permutation in Coding Interviews?

When preparing for a tech interview, you might encounter questions involving arrays and permutations. One common problem asks you to build an array based on a given permutation. This task is straightforward, but understanding the logic behind it is essential. In this article, I'll explain what this problem is about, how to approach it, and provide a clear example with code.

The problem usually states: Given an array where each element is a permutation of numbers from 0 to n-1, create a new array where each element at index i is assigned the value of nums[nums[i]]. This might seem confusing at first, but think of it as applying the permutation twice.

Understanding the Problem

Suppose you have an array called nums, filled with integers from 0 to n-1, and each number appears exactly once. This array represents a permutation of these numbers. Your goal is to generate a new array result such that:

Html

for every index i. Essentially, you're "mapping" each element through the permutation twice.

Why is this important?

This problem tests your understanding of array indexing and how to manipulate data based on array values. It also examines your ability to write efficient code using basic loops and array access.

Approach to the Solution

The main idea is to iterate through each index in nums, use nums[i] as an index to find the next value, and assign that to the result array. This process takes constant time for each element, so the overall time complexity is O(n).

Here's a step-by-step plan:

  1. Create an empty array called result with the same size as nums.
  2. Loop through each index i in nums.
  3. For each index, find nums[nums[i]].
  4. Assign this value to result[i].

Example

Let's consider an example:

Python
  • For i=0, nums[nums] = nums = 0.
  • For i=1, nums[nums] = nums = 1.
  • For i=2, nums[nums] = nums = 2.
  • For i=3, nums[nums] = nums = 4.
  • For i=4, nums[nums] = nums = 5.
  • For i=5, nums[nums] = nums = 3.

The resulting array is:

Python

This array effectively maps each position to a permuted index according to the permutation rules.

Sample Code

Here's a simple Python implementation of this approach:

Python

Optimization Tips

  • Since the problem asks to modify the array in place or create a new one, you can also do it without additional space if required, but the above method is clear and efficient.
  • Sometimes, in interview settings, space optimization is also valuable, which can involve more complex in-place algorithms.

Understanding this problem helps in tackling related array and permutation questions, and practicing it builds a strong foundation for array manipulation tasks in interviews.

Create your AI Agent

Automate customer interactions in just minutes with your own AI Agent.

Featured posts

Subscribe to our newsletter

Achieve more with AI

Enhance your customer experience with an AI Agent today. Easy to set up, it seamlessly integrates into your everyday processes, delivering immediate results.