← Interval & Math

Micro-Drill #117 — Spiral order traversal

Interval & Math Target: 15s

Spiral traversal uses shrinking boundaries: top, bottom, left, right.

res = []
t, b, l, r = 0, len(m)-1, 0, len(m[0])-1
while t <= b and l <= r:
    for c in range(l, r+1): res.append(m[t][c])
    t += 1
    for c in range(t, b+1): res.append(m[c][r])
    r -= 1

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #116 Micro #118 →