Be the first user to complete this post

  • 0
Add to List
Medium

13. Print All Elements of Two Dimensional Array in Spiral Order

Objective: This question was asked in Amazon interview for the Software development Engineer position, Write an algorithm to print all the elements of two dimensional array in spiral.

Input: Two dimensional array
Output: All array elements printed in spiral.

Print 2D array in Spiral

Approach:

  • Start printing from first row.
  • Print row and columns, forward and backward alternatively
  • With every iteration of (either row or column), reduce the size of an row or column by 1
  • Call recursively

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30