Ctrl K

Snake Walk, Every Other Cell

Easy
You get a rectangular table grid of positive integers. Walk through it like a snake: the first row left to right, the second right to left, the third left to right again, and so on down to the last row. Along the way, write down every other number: record the first cell of the path, skip the second, record the third, and so on. The alternation carries over from one row to the next and is never reset. Return the recorded numbers in the order you visited them.
Example 1
grid=[[4, 8, 15], [16, 23, 42]] → [4, 15, 23]

The full snake path is 4, 8, 15, then right to left 42, 23, 16. Taking every other cell from the first gives 4, 15, 23.

Example 2
grid=[[7, 1], [9, 3], [5, 6]] → [7, 3, 5]

The path is 7, 1, 3, 9, 5, 6. Every other cell: 7, 3, 5.

Example 3
grid=[[2, 4, 6, 8], [1, 3, 5, 7]] → [2, 6, 7, 3]
Constraints
  • 2 ≤ len(grid) ≤ 50
  • 2 ≤ len(grid[i]) ≤ 50, and all rows have the same length
  • 1 ≤ grid[i][j] ≤ 2500
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepack the Tray EasyMirror and Negate a Bitmap EasySwap Rows and Columns EasyPawns the Rook Can Take EasyRolling Grid Easy
loading Python…
Input
grid=[[4, 8, 15], [16, 23, 42]]
Expected
[4, 15, 23]

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.