Wipro Coding Question 6

Matrix Traversal 

Matrix Traversal is one of the coding question asked in previous wipro Elite NTH Exam. In this article, we will discuss about the Matrix Traversal problem with their solution in C++ and Java. 

Wipro Coding Question

Question 6

Given an integer matrix of size N x N. Traverse it in a spiral form.

Format:
Input:
The first line contains N, which represents the number of rows and columns of a matrix. The next N lines contain N values, each representing the values of the matrix.

Output:
A single line containing integers with space, representing the desired traversal.

  • Constraints: 0 < N < 500

Example 1:
Input:
3
1 2 3
4 5 6
7 8 9

Output:
1 2 3 6 9 8 7 4 5

Example 2:
Input
3
1 2 3
4 5 6
7 8 9

Output:
1 2 3 6 9 8 7 4 5

One comment on “Wipro Coding Question 6”


  • Prasanth

    f=[]
    l = [
    [12,7],
    [4 ,5]]
    l3=l.copy()
    for h in range(0,len(l)//2):

    l1=[]
    final=[]

    final=final+l[0]

    for i in range(0,len(l)):
    if(i==0) or (i==len(l)-1):
    pass
    else:
    final.append(l[i][-1])

    final=final+l[-1][::-1]

    for i in range(0,len(l)):
    if(i==0) or (i==len(l)-1):
    pass
    else:
    l1.append(l[i][0])
    final=final+l1[::-1]
    f=f+final

    for i in range(0,len(l)):
    l[i].pop(0)
    l[i].pop(-1)

    l.pop(0)
    l.pop(-1)
    print(l)
    if(len(l3)%2!=0):
    f=f+l[0]
    print(f)