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

Spiral Order Matrix Traversal

MediumPython

Problem

Given an m x n matrix, how would you return all elements in spiral order?

Implement spiral_order(matrix), which accepts a rectangular list of lists and returns a flat list containing every element exactly once, traversed clockwise from the top-left corner. Return an empty list when the matrix is empty.

Input and Output

matrix is a rectangular list of lists. Return a list in clockwise spiral order.

Constraints

  • The matrix is rectangular.
  • 0 <= m, n <= 100.
  • Each element is an integer.
  • Every matrix element must appear exactly once in the output.

Function Signature

def spiral_order(matrix):
Interviewer

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