Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Flatten Nested Arrays and 2D Traversal
00:00
5 left

Flatten Nested Arrays and 2D Traversal

HardPython

Problem

In a Zemoso Technologies data-transformation pipeline, implement one function that processes nested array data and a square matrix. The function must flatten the nested array in left-to-right depth-first order, return the matrix elements in clockwise spiral order, and rotate the matrix 90 degrees clockwise in place.

The nested array may contain integers or arrays nested to arbitrary depth. The matrix contains integers and must remain square. Return a dictionary with keys flattened, spiral, and rotated. The rotated value must represent the matrix after in-place clockwise rotation. Assume the flattened array and matrix contain the same number of values, although the two results are independent.

You should avoid recursion for flattening because nesting depth may be large. The matrix rotation should use O(1) auxiliary space beyond the returned results.

Formal Specification

Input: nested, a recursively nested list of integers, and matrix, an n x n list of lists of integers. Output: a dictionary containing three lists: the flattened values, spiral traversal values, and rotated matrix.

Constraints

  • 0 <= total number of integers in nested <= 10^5
  • Nesting depth can be as large as 10^5
  • 1 <= len(matrix) <= 500
  • The matrix is square and rectangular
  • The number of nested integers equals len(matrix) * len(matrix)

Function Signature

def flatten_and_transform(nested, matrix):
Interviewer

Your question is Flatten Nested Arrays and 2D Traversal. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.