# Wipro NTH Coding Sample Questions

## Sample Practice Coding Questions for Wipro NTH

On this page you are going to see Wipro NTH Coding Questions along with Answers to practice in following languages:

• C
• C++
• Java
• Python

## Wipro NTH Coding Question -1 :

NOTE:- Please comment down the code in other languages as well below –

Ques:  Write a program to check if two given matrices are identical

Solution in C:

```#include<stdio.h>
#define N 4
// This function returns 1 if A[][] and B[][] are identical
// otherwise returns 0
int areSame (int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if (A[i][j] != B[i][j])
return 0;
return 1;
}
int main ()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}
};
int B[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}
};
if (areSame (A, B))
printf ("Matrices are identical ");
else
printf ("Matrices are not identical");
return 0;
}```

Solution in C++:

```#include<bits/stdc++.h>
using namespace std;
#define N 4
// This function returns 1 if A[][] and B[][] are identical
// otherwise returns 0
int areSame (int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if (A[i][j] != B[i][j])
return 0;
return 1;
}
int main ()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}
};
int B[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}
};
if (areSame (A, B))
cout<<"Matrices are identical";
else
cout<<"Matrices are not identical";
return 0;
}```

Solution in Java:

```import java.util.*;
class Main
{
static int size=4;
public static boolean areSame(int A[][], int B[][])
{
int i,j;
for(i=0;i < size;i++)
{
for(j=0;j < size;j++)
if(A[i][j]!=B[i][j])
return false;
}
return true;
}
public static void main(String[] args)
{
int A[][]={{1,1,1,1},{2,2,2,2},{3,3,3,3,},{4,4,4,4}};
int B[][]={{1,1,1,1},{2,2,2,2},{3,3,3,3,},{4,4,4,4}};
if(areSame(A,B))
{
System.out.println("Matrices are identical");
}
else
System.out.println("Matrices are not identical");
}
}```

Solution in Python:

```a = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
b = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
if a==b:
print("Matrices are identical")
else:
print("Matrices are not identical")```

## Wipro NTH Coding Question – 2

Print a given matrix in spiral form

Ques: Given a 2D array, print it in spiral form. See the following examples.

NOTE:-  Please comment down the code in other languages as well below .

```Input:
1    2   3   4
5    6   7   8
9   10  11  12
13  14  15  16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Input:
1   2   3   4  5   6
7   8   9  10  11  12
13  14  15 16  17  18
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11```

Solution in C:

```#include <stdio.h>
#define R 3
#define C 6
void spiralPrint (int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf ("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf ("%d ", a[i][n - 1]);
}
n--;
/* Print the last row from the remaining rows */
if (k < m)
{
for (i = n - 1; i >= l; --i)
{
printf ("%d ", a[m - 1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m - 1; i >= k; --i)
{
printf ("%d ", a[i][l]);
}
l++;
}
}
}
/* Driver program to test above functions */
int main ()
{
int a[R][C] = { {1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}
};
spiralPrint (R, C, a);
return 0;
}```

Solution in C++:

```#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 6
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n) {
/* Print the first row from
the remaining rows */
for (i = l; i < n; ++i) {
cout << a[k][i] << " ";
}
k++;
/* Print the last column
from the remaining columns */
for (i = k; i < m; ++i) {
cout << a[i][n - 1] << " ";
}
n--;
/* Print the last row from
the remaining rows */
if (k < m) { for (i = n - 1; i >= l; --i) {
cout << a[m - 1][i] << " ";
}
m--;
}
/* Print the first column from
the remaining columns */
if (l < n) { for (i = m - 1; i >= k; --i) {
cout << a[i][l] << " ";
}
l++;
}
}
}
/* Driver program to test above functions */
int main()
{
int a[R][C] = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };
spiralPrint(R, C, a);
return 0;
}```

Solution in Java:

```import java.io.*;
class Main
{
// Function print matrix in spiral form
static void spiralPrint (int m, int n, int a[][])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
// Print the first row from the remaining rows
for (i = l; i < n; ++i)
{
System.out.print (a[k][i] + " ");
}
k++;
// Print the last column from the remaining columns
for (i = k; i < m; ++i)
{
System.out.print (a[i][n - 1] + " ");
}
n--;
// Print the last row from the remaining rows */
if (k < m)
{
for (i = n - 1; i >= l; --i)
{
System.out.print (a[m - 1][i] + " ");
}
m--;
}
// Print the first column from the remaining columns */
if (l < n)
{
for (i = m - 1; i >= k; --i)
{
System.out.print (a[i][l] + " ");
}
l++;
}
}
}
// driver program
public static void main (String[]args)
{
int R = 3;
int C = 6;
int a[][] = { {1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}
};
spiralPrint (R, C, a);
}
}```

Solution in Python:

```def spiralOrder(arr):
ans=[]
while arr:
ans+=arr.pop(0)
arr= (list(zip(*arr)))[::-1]
return ans
arr=[[1, 2, 3, 4, 5, 6],[7, 8, 9, 10, 11, 12],[13, 14, 15, 16, 17, 18]]
print(spiralOrder(arr))```

## Wipro NTH Coding Question – 3

Ques:  Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the most efficient way to return the row with the maximum number of 0’s.

{1,1,1,1},
{1,1,0,0},
{1,0,0,0},
{1,1,0,0},

Solution in C:

```#include<stdio.h>
#include<math.h>
int main()
{
int r,c,a,m=2147483647,ans=-1;
scanf("%d %d",&r,&c);
for(int i=0;i< r;i++)
{
int sum=0;
for(int j=0;j< c;j++)
{
scanf("%d",&a);
sum+=a;

}
if(m>sum)
{
m=sum;
ans=i+1;
}
}
printf("%d",ans);
}```

Solution in C++:

```#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int r, c, a, m = 2147483647, ans = -1;
cin >> r >> c;
for (int i = 0; i < r; i++)
{
int sum = 0;
for (int j = 0; j < c; j++)
{
std::cin >> a;
sum += a;
}
if (m > sum)
{
m = sum;
ans = i + 1;
}
}
cout << ans << std::endl;
return 0;
}```

Solution in Java:

```import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
int c = scanner.nextInt();
int a, m = Integer.MAX_VALUE, ans = -1;

for (int i = 0; i < r; i++) {
int sum = 0;
for (int j = 0; j < c; j++) {
a = scanner.nextInt();
sum += a;
}
if (m > sum) {
m = sum;
ans = i + 1;
}
}
System.out.println(ans);
}
}```

Solution in Python:

```r, c = map(int, input().split())
m = float('inf')
ans = -1

for i in range(r):
row_sum = sum(map(int, input().split()))
if row_sum < m:
m = row_sum
ans = i + 1

print(ans)```

## Wipro NTH Coding Question – 4

Ques: A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit.

```Input : limit = 20
Output : 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20```

Simple Solution is to generate these triplets smaller than given limit using three nested loop. For every triplet, check if Pythagorean condition is true, if true, then print the triplet. Time complexity of this solution is O(limit3) where ‘limit’ is given limit.

An Efficient Solution can print all triplets in O(k) time where k is number of triplets printed. The idea is to use square sum relation of Pythagorean triplet, i.e., addition of squares of a and b is equal to square of c, we can write these number in terms of m and n such that,

```       a = m2 - n2
b = 2 * m * n
c  = m2 + n2
because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m2 * n2
c2 = m4 + n4 + 2* m2 * n2
```

We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m and n and can generate these triplets

Solution in C:

```#include<stdio.h>
#include<math.h>
void pythagoreanTriplets (int limit)
{
// triplet:  a^2 + b^2 = c^2
int a, b, c = 0;
int m = 2;
// Limiting c would limit all a, b and c
while (c < limit)
{
// now loop on j from 1 to i-1
for (int n = 1; n < m; ++n)
{
// Evaluate and print triplets using
// the relation between a, b and c
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
printf ("%d %d %d \n", a, b, c);
}
m++;
}
}
int main ()
{
int limit = 20;
pythagoreanTriplets (limit);
return 0;
}```

Solution in C++:

```#include <bits/stdc++.h>
using namespace std;
void pythagoreanTriplets(int limit)
{
int a, b, c = 0;
int m = 2;

while (c < limit)
{
for (int n = 1; n < m; ++n)
{
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
cout << a << " " << b << " " << c << "\n";
}
m++;
}
}
int main()
{
int limit = 20;
pythagoreanTriplets(limit);
return 0;
}```

Solution in Java:

```import java.util.*;
public class Main {
public static void pythagoreanTriplets(int limit) {
int a, b, c = 0;
int m = 2;

while (c < limit) {
for (int n = 1; n < m; ++n) {
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
System.out.println(a + " " + b + " " + c);
}
m++;
}
}

public static void main(String[] args) {
int limit = 20;
pythagoreanTriplets(limit);
}
}```

Solution in Python:

```def pythagoreanTriplets(limit):
a, b, c = 0, 0, 0
m = 2

while c < limit:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limit:
break
print(a, b, c)

m += 1

limit = 20
pythagoreanTriplets(limit)```