Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Spiral Matrix Printing
00:00
5 left

Spiral Matrix Printing

MediumPython

Problem

How would you print a matrix in spiral order?

Implement spiral_order(matrix) to return the elements of a rectangular matrix in clockwise spiral order, starting at the top-left corner. The input is a list of row lists, and the output must be a flat list. Handle single-row, single-column, rectangular, and empty matrices without revisiting elements.

Constraints

  • 0 <= len(matrix) <= 100
  • If matrix is non-empty, 1 <= len(matrix[0]) <= 100
  • All rows have the same length
  • Matrix values are integers

Function Signature

def spiral_order(matrix):
Interviewer

Your question is Spiral Matrix Printing. 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.