Print All Elements of Two Dimensional Array in Spiral

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.
Example :

Print 2D array in Spiral

Print 2D array in Spiral

Input: Two dimensional array

Output: All array elements printed in spiral.


  • 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

Complete Code:

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

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

  • Hari Kiran

    Awesome tutorials. Please make more. I am a huge fan of your tutorials .

    • Sumit Jain

      Thanks Hari, Your comment is motivation for me πŸ™‚

  • Nishant Dehariya

    really awesome i find everything easy and better than geeksforgeeks although i m c++ guy.

%d bloggers like this: