Prepin
Log in

Duplicate a Spiral Matrix

Approach / Explanation

This question requires you to create an algorithm or function that can replicate a specific type of 2-dimensional array or matrix, known as a spiral matrix. A spiral matrix is one that fills its entries in a spiral fashion. For example, in an NxN matrix, the first row is populated with numbers from 1 to N, followed by filling the last column with numbers from N+1 to 2N , and continuing this pattern in a spiral manner. To effectively address this question, you should consider the following key points: Keep track of the matrix boundaries: top, bottom, left, and right. Adhere to a specific traversal pattern: Begin at the top-left corner, move right until the end, and then increment the top boundary, Next, move down until the end, and decrement the right boundary, Then, move left until you reach the start, and decrement the bottom boundary, Finally, move up until you reach the start again, and increment the left boundary, then repeat the process.

Suggested Answer

To address this problem using Python, the function will be structured as follows: def spiral_copy ( inputMatrix ) : result = [ ] while inputMatrix : result += inputMatrix . pop ( 0 ) if inputMatrix and inputMatrix [ 0 ] : for row in inputMatrix : result . append ( row . pop ( ) ) if inputMatrix : result += inputMatrix . pop () [ : : - 1 ] if inputMatrix and inputMatrix [ 0 ] : for row in inputMatrix [ : : - 1 ] : result . append ( row . pop ( 0 ) ) return result This Python function removes the top row and adds it to the result list. After processing the top row, it takes the rightmost element from the remaining rows and appends it to the result list. If there are still rows left, it adds the reversed bottom row to the result and finally, takes the leftmost element from the remaining rows to append it to the result (also in reversed order). This process continues until all elements from the original matrix have been processed.

Alternative Answer

The JavaScript code will be organized as follows: function spiralCopy ( matrix ) { const arr = [ ] ; while ( matrix . length ) { arr . push ( ... matrix . shift ( ) ) ; for ( let i = 0 ; i < matrix . length ; i ++ ) { let val = matrix [ i ] . pop ( ) ; if ( val ) { arr . push ( val ) ; matrix [ i ] . reverse ( ) ; } } matrix . reverse ( ) ; } return arr ; } This JavaScript function operates in a manner similar to the Python function, albeit with some minor syntactical variations. It retrieves the first row from the matrix using shift() and adds it to the result array. Subsequently, it pulls the last element from the remaining rows and appends them to the result. Finally, it reverses the remaining matrix and continues the process until all elements have been processed.

Question Details

Difficulty & Category

Medium
algorithms

Product

AI Candidate AgentCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.