Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.