Problem 213915 · medium · Phase 02 Linear Data Structures

Walk the Border

2d-lists · border · traversal

Given a rectangular grid grid, return the values on its border in clockwise order, starting at the top-left corner: along the top row, down the right column, back along the bottom row, and up the left column. Every border cell appears exactly once.

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]]
Output: [1, 2, 3, 6, 9, 8, 7, 4]

Input:  grid = [[1, 2, 3]]
Output: [1, 2, 3]

Input:  grid = [[1],
                [2],
                [3]]
Output: [1, 2, 3]

Constraints

  • 1 <= rows, cols <= 100

Goals

  • Traverse the outer ring of a grid in clockwise order
  • Avoid repeating corner cells and handle a single row or column
Starting Python…