GeeksforGeeks » Interview Questions
Interview Question for Software Engineer/Developer about Arrays
(2 posts)-
there is a m*n array of integrs, you have to traverse it spirally. e.g
1 2 3 4
5 6 7 8
9 0 1 2
result : 1,2,3,4,8,2,1,0,9,5,6,7 -
Have 4 int variables for storing the boundary co-ordinates.
tl=0,0//top left
bl=rows,0//bottom left
tr=0,columns//top right
br=rows,colums//bottom rightuse 4 for loops ( with the above variables as boundary conditions) for traversing in each direction.( east -> south -> west -> north)
after each for loop, just update the boundary variables appropriately so that the for loop doesnt go into the same path again.
when all the 4 for loops cant run any further( boundary condition reached) break from the external while(1) loop.
Reply
You must log in to post.