Saturday, 22 July 2017

Top 100 Networking Interview Questions click here
Basic Networking Interview Questions with answer  click here
Top 500 Java Interview Questions and Answers click here

Friday, 21 July 2017

Write program for Lower Triangula Matrix

Lower triangle matrix
Write a program to print the following pattern for a given integer.

Sample Input 1:
3
Sample Output 1:

1
6 2
5 4 3 
                                          C-Solution
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,i,c=1,j=0,ans;
scanf("%d",&n);
int a[n][n];
if(n%2==0){ans=(pow(n,2)/2)+n/2;}
else
ans=(n-(n/2))*n;

int m=n-1,p=0,l=0,k;
while(c-1<ans)
{
for(i=l+p;i<=m&&c-1<ans;i++)
a[i][i-l]=c++;
++l;
for(j=m-l;j>=p&&c-1<ans;j--)
a[m][j]=c++;
for(k=m-1;k>l+p-1&&c-1<ans;k--)
a[k][p]=c++;
m--;p++;
}
for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
{ printf("%d ",a[i][j]);}
printf("\n");
}
return 0;
}

Write a C program to print the elements of a square matrix in spiral order from the center.

                                             Spiral Matrix 
Assume the size of the matrix is odd.
Sample Input:
3
1 2 3
4 5 6
7 8 9
Sample Output:
5 4 7 8 9 6 3 2 1 
                                            C-Solution
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);int a[n][n],b[n*n],c=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);//printf("%d ",a[i][j]);
}
}

int top=0,bottom=n-1,left=0,right=n-1,direction=0;
while(top<=bottom&&left<=right)
{
if(direction==0)
{
for(i=left;i<=right;i++){
b[c]=a[top][i];c++;
//printf("%d ",b[c-1]);
}
top++;
}
else if(direction==1)
{
for(i=top;i<=bottom;i++)
{
b[c]=a[i][right];c++;
//printf("%d",b[c-1]);
}
right--;
}
else if(direction==2)
{
for(i=right;i>=left;i--)
{
b[c]=a[bottom][i];c++;
// printf("%d",b[c-1]);
}bottom--;
}
else if(direction==3)
{
for(i=bottom;i>=top;i--)
{
b[c]=a[i][left];c++;
// printf("%d",b[c-1]);
}left++;


}
direction=(direction+1)%4;

}
for(i=c-1;i>=0;i--)
{ printf("%d ",b[i]);
}
return 0;
}

Write a C program to print circular pattern for a given integer.

Write a C program to print the following pattern for a given integer.
Sample Input 1:
4
Sample Output 1:
4444444
4333334
4322234
4321234
4322234
4333334
4444444

                                       C-Solution
#include<stdio.h>
#include<stdlib.h>
int findmax(int a,int b)
{
if(a>b)return a;
    else return b;
}
int main()
{
int n,i,j;
scanf("%d",&n);
int m=(n*2)-1;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d",1+findmax(abs(n-i-1),abs(n-j-1)));
}printf("\n");
}
return 0;
}

Write a program to divide an array into 2 part such that the average of each part should be same.

Divide an array
Write a program to divide an array into 2 part such that the average of each part should be same.
Assume that there is only one solution possible.
[Note: Print the output in ascending order. Always the first element is in the first part of the array.]
 
Sample Input:
5
3
4
2
9
6
Sample Output:
3 9

2 4 6 

                                            C-Solution
#include<stdio.h>
int main()
{
int n,sum=0,i,j,t,t1;
scanf("%d",&n);int a[n],k=0,b[n],c[n];
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
sum=sum+a[i];}
sum=sum/2;
t1=a[0];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]>a[j])
{ t=a[i];a[i]=a[j];a[j]=t;}
t=0;
for(i=0;i<n;i++)
{
if(a[i]<=sum)
{
sum=sum-a[i];
b[k++]=a[i];
}
else
c[t++]=a[i];

}
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
    if(b[i]<b[j])
    {sum=b[i];b[i]=b[j];b[j]=sum;}
 }
}
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
     if(c[i]<c[j])
     {sum=c[i];c[i]=c[j];c[j]=sum;}
   }
}
int flag=0;
for(i=0;i<k;i++)
{  
 if(b[i]==t1)
{flag=1;break;}
}
if(flag)
{
for(i=0;i<k;i++)
printf("%d ",b[i]);printf("\n");
for(i=0;i<t;i++)
printf("%d ",c[i]);
}
else
{
for(i=0;i<t;i++)
printf("%d ",c[i]);printf("\n");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
return 0;
}



Write a program to find the sum of the path which has the maximum length, given a square matrix.

Maximum path sum
Google is creating a toy robo can travel within a given square grid. Each cell in the grid has a number. To prove the intelligence of the company, Google decides to make the robo travel in a path which has the maximum sum when the number in the path are added.
To start with, the project manager has asked the programmers to fix the source as coordinate (0,0) and destination as coordinate (n-1,n-1), where 'n' is the size of the square grid. And also the robo can travel in only two directions, right, down and diagonally right, i.e., if the current position is (x,y), the robo's next move can be either (x+1,y), (x,y+1) or (x+1,y+1).

Write a program to find the sum of the path which has the maximum length, given a square matrix.
Sample Input:
4
1 4 5 21
3 22 1 5
22 21 1 6
3 32 2 2
Sample Output:
84
Explanation for the sample input and output:
This is the input matrix.
1 4 5 21
3 22 1 5
22 21 1 6
3 32 2 2
This is the path with maximum sum.

                                                                C-Solution
#include<stdio.h>
#include<stdlib.h>
int max(int x, int y, int z);
int maxSum(int cost[50][50], int m, int n)
{
if (n < 0 || m < 0)
return 0;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n] +
 max(maxSum(cost,m-1,n-1),maxSum(cost,m-1,n),maxSum(cost,m,n-1));
}

int max(int x, int y, int z)
{
if (x > y)
return (x > z)? x : z;
else
return (y > z)? y : z;
}

int main()
{
int n,i,j;
scanf("%d",&n);int a[50][50];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" %d ", maxSum(a,n,n));
return 0;
}

Sudoku --- Valid configuration or not

 Sudoku --- Valid configuration or not



Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. Each 9x9 matrix is partially completed at the start of game play and typically has a unique solution.
Given a completed NxN Sudoku matrix, your task is to determine whether it is a valid solution. A valid solution must satisfy the following criteria:
  • Each row contains each number from 1 to N, once each.
  • Each column contains each number from 1 to N, once each.
  • Divide the NxN matrix into N non-overlapping sqrt(N)xsqrt(N) sub-matrices. Each sub-matrix contains each number from 1 to N, once each.

Write a program to just check if the given matrix is a valid sudoku solution.

Sample Input 1:
4
1 2 3 4
3 4 1 2
2 1 4 3
4 3 2 1

Sample Output 1:

yes
                                               C - Solution
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n;
int checkgrid(int **a,int row,int col,int check1,int sum)
{ int i,j;
for(i=row;i<(row+sqrt(n));i++)
for(j=col;j<(col+sqrt(n));j++)
{
if(!(a[i][j]>=1 && a[i][j]<=n)){return 1;}
check1=check1+a[i][j];
}
if(sum!=check1)return 1;
return 0;
}
int main()
{ int sum=0,check1=0,check2=0,flag=0,i,j;
scanf("%d",&n);
if(n%2==0) sum=(pow(n,2)/2)+(n/2);
else sum=(n-(n/2))*n;
int **a=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)a[i]=malloc(n*sizeof(int));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{ check1=0;check2=0;
for(j=0;j<n;j++)
{
if(!(a[i][j]>=1&&a[i][j]<=n)){printf("no");
                 return 0;}
check1+=a[i][j];check2+=a[j][i];
}
if(check1!=sum||check2!=sum) {flag=1;break;}
}
if(flag!=1)
{
for(i=0;i<n;i=i+sqrt(n))
for(j=0;j<n;j=j+sqrt(n))
{
flag=checkgrid(a,i,j,0,sum);
              if(flag){printf("no");return 0;}
	         }
}
if(flag)printf("no");
else printf("yes");
return 0;
}

Sort the numbers based on the weight in decreasing order

Number Weight
Given a set of numbers like <10, 36, 54,89,12> write a program to find sum of weights based on the following conditions
    1. 5 if a perfect square
    2. 4 if multiple of 4 and divisible by 6
    3. 3 if even number


Print the output as follows.
(10,its_weight)(36,its weight)(89,its weight)

Note: Sort the numbers based on the weight in decreasing order. If 2 weights are same, display the numbers based on increasing order.

Sample Input:
5
10 36 54 89 12
Sample Output:
(36,12)(12,7)(10,3)(54,3)(89,0)


                                           C-Solution 
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,t1,t2;
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));
int *b=(int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<n;i++)
{
if(sqrt(a[i])*sqrt(a[i])==a[i])
{
b[i]+=5;
}
if(a[i]%4==0&&a[i]%6==0)
{
b[i]+=4;
}
if(a[i]%2==0)
{
b[i]+=3;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i]<b[j])
{
t1=a[i];t2=b[i];
a[i]=a[j];b[i]=b[j];
a[j]=t1;b[j]=t2;
}
else if(b[i]==b[j])
{
t1=a[i];a[i]=a[j];a[j]=t1;
}
}
}
for(i=n-1;i>=0;i--)
{
printf("(%d,%d)",a[i],b[i]);
}
return 0;
}

ZOHO Interview Questions PDF Download

ZOHO C Aptitude click here

ZOHO 2nd Round Questions click here

Wednesday, 19 July 2017

Program to Find Number of Grandchildren - Zoho Programming Test Question


                                                 Grand Children
Here we will see a c program to find number of grandchildren, great 
grandchildren great-great grandchildren and so-on of a given person. In 
this program, the input is a table with two columns, 'Child' and 'Father'.
Then we enter a name whose number grandchildren, great grandchildren
etc is to be calculated. We do not have to count children. An example is:
Given a two dimensional array of strings like
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”> 
  
Where in each row, the first string is “child”, second string is 
“Father”. And given “ronaldo” we have to find his no of grandchildren. 
Here “ronaldo” has total 3 grandchildren (2 grandchildren: wayne and shaw ; 
a great grandchild luke). So our output should be 3.
write a program to find the number of grandchilden for a given name.                                       

                                                   C - Solution

#include <string.h>
#include <stdio.h>


int main()
char a[10][100],b[10][100],str[100];
{
int num,k;
scanf("%d",&num);
scanf("%s",&str);
for(int i=0;i<num;i++)
{
scanf("%s%s",&a[i],&b[i]);
if(strcmp(str,b[i]) == 0)
k = i;
}
int count = 0;
for(int i=0;i<num;i++)
{
if(strcmp(a[k],b[i]) == 0)
count++;
}
printf("%d",count);
return 0;

}
(or)

#include<stdio.h>
#include<string.h>

int n;
char name[20];
struct reln
{
char child[20];
char father[20];
}r[10];
int count=0;

void countChildren(char name[])
{

    int j;
   for(j=0;j<n;j++)
        {
        if(strcmp(name,r[j].father)==0)
            {
            count++;
            countChildren(r[j].child);
            }
        }
    }
void main()
{
 int i;
printf("\nEnter the number of inputs: ");
scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%s",r[i].child);
    scanf("%s",r[i].father);
    }
printf("\nEnter name of the one whose no. of grandchildren is needed: ");
scanf("%s",name);
for(i=0;i<n;i++)
    {
    if(strcmp(r[i].father,name)==0)
        countChildren(r[i].child);
    }

printf("\nNo .of grandchildren of %s=%d",name,count);
}
scanf("%s",name);
for(i=0;i<n;i++)
    {
    if(strcmp(r[i].father,name)==0)
        countChildren(r[i].child);
    }
printf("\nNo .of grandchildren of %s=%d",name,count);
}
                                       

                                                  JAVA - Solution

import java.util.Scanner;
import java.util.ArrayList;
class Main  {
 public static void main(String args[]) { 
  Scanner scan=new Scanner(System.in);
  int n=scan.nextInt();scan.nextLine();
  ArrayList< String > arr=new  ArrayList<>();
  int i,j,flag;
  String[] input=new String[n];
  String[] father=new String[n];
  String[] child=new String[n];
   for(i=0;i<n;i++)
  {
    input[i]=scan.nextLine();
    father[i]="";
    child[i]="";
    flag=0;
    for(j=0;j<input[i].length();j++)
    {
    if(input[i].charAt(j)==',')
       flag=1;
    if(((input[i].charAt(j)>='a'&&input[i].charAt(j)<='z')||(input[i].charAt(j)>='A' &&
                input[i].charAt(j)<='Z'))&&flag==0)
        child[i]=child[i]+input[i].charAt(j);  

    else  if(((input[i].charAt(j)>='a'&&input[i].charAt(j)<='z')||
                 (input[i].charAt(j)>='A'&&input[i].charAt(j)<='Z'))&&flag==1)
        father[i]=father[i]+input[i].charAt(j);
    }
  }
  String search=scan.nextLine();
  for(i=0;i<n;i++)
  {
     if(father[i].equals(search))
     {
         arr.add(child[i]);
     }
  }
  flag=0;
  for(i=0;i<arr.size();i++)
  {
     for(j=0;j<n;j++)
     {
       if(father[j].equals(arr.get(i)))
        flag++;  
     }
   }
  System.out.println(flag);
 }
}

Sample Input:
 4
 <”luke”, “shaw”>
 <”wayne”, “rooney”>
 <”rooney”, “ronaldo”>
 <”shaw”, “rooney”>
 ronaldo
Sample Output:
 2