Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
90-Degree Matrix Rotation
00:00
5 left

90-Degree Matrix Rotation

MediumPython

Problem

Meta's image-processing pipelines may apply a sequence of transformations to a square pixel matrix. Implement a command executor that modifies the matrix in order and returns its final state.

Supported commands are:

  1. rotate90: Rotate the square matrix 90 degrees clockwise in place by transposing it, swapping rows and columns, and then reversing every row.
  2. swap_rows i j: Swap rows i and j.
  3. swap_cols i j: Swap columns i and j across every row.
  4. reverse_row i: Reverse the values in row i.

The command strings are guaranteed to be valid. Return the same matrix object after all commands are executed.

Formal Specification

Given matrix, a non-empty n x n list of lists of integers, and commands, a list of command strings, return the transformed n x n matrix. Indices are zero-based. All operations must modify the supplied matrix rather than constructing a replacement matrix for the primary solution.

Constraints

  • 1 <= n <= 500
  • matrix is a non-empty n x n matrix
  • All matrix values are integers
  • 0 <= i, j < n
  • 1 <= len(commands) <= 10^4
  • Every command is valid

Function Signature

def execute_matrix_commands(matrix, commands):
Interviewer

Your question is 90-Degree Matrix Rotation. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.