Search This Blog

Showing posts with label interview question. Show all posts
Showing posts with label interview question. Show all posts

Program to Find Number of Grandchildren - Zoho Programming Test Question

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.


#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);
}

Program to Display String with Odd Number of Letters in the shape of letter 'X' - Zoho Programming Test Question

One of the previous questions in the programming round of Zoho recruitment process was to write a C program to display any string with odd number of letters as follows, in the shape of letter 'X'.

Print the word with odd number of letters as
P         M
 R      A
   O  R
     G
  O    R
 R       A
P          M 



#include<stdio.h> #include<string.h> #include<stdlib.h> void main() { int n,i,j,k,l; char str[20]; printf("Enter a string with odd number of letters."); scanf("%s",str); if((n=strlen(str))%2==0)     {     printf("\nNot odd");     exit(0);     } for(i=0;i<n;i++)     {     printf("\n");     for(j=0;j<n;j++)         {         if(i==j||j==n-1-i)             printf("%c",str[j]);         else             printf(" ");         }     } }

Four Different Methods or Tricks to Swap Two Numbers Without Using a Third Variable

In this post, we will see four different methods in c to swap two variables without using a third variable. This type of questions are often asked in technical interviews. Here we will see four different C programs to swap two numbers without a third temporary variable. Using a third variable is not an overhead. But still this kinda questions are asked in technical interviews for programming jobs to test your knowledge in the language.

Trick 1

In the first c program to swap two numbers without a third variable, we swap the values of the two variables in 3 steps which are simple addition and subtractions.

#include<stdio.h>
void main(){
    int a=5,b=10;
    a=b+a; //now a =15
    b=a-b; //now b= 5
    a=a-b; // now a=10 (done)
    printf("a= %d\nb=  %d",a,b);
}


Trick 2

In the second c program to swap two numbers without a third variable, we swap them in a single statement. The statement contains an assignment, addition and subtraction.

#include<stdio.h>
void main(){
    int a=10,b=30;
    a=a+b-(b=a);
    //10+30-(10)  a becomes 30
    // b is assigned with 10 in the same line of code
    printf("a= %d\nb=  %d",a,b);
}

Trick 3

In the third c code, we swap the numbers using bitwise XOR operation. The bitwise operator ^ 
is used in the program. The binary representation of the numbers, the bitwise xor operation on them and the result are shown in the c program as comments. The program is as follows:

#include<stdio.h>
void main(){
    int a=8,b=12;

   /* a: 8 : 01000
       b:12 : 01100
    */
   
    a=a^b; 

    /*      01000 ^
            01100
       a=   00100 (4)
    */


    b=a^b;

    /*      00100 ^
            01100
       b=   01000 (8)
    */

a=b^a;

    /*      01000 ^
            00100
       a=   01100 (12)
    */



    printf("a= %d\nb=  %d",a,b);
}

Trick 4

In the fourth method to swap two numbers without a temporary variable, we swap the numbers using a combination of addition, subtraction and bitwise NOT (inversion) operation. The bitwise operator 
is used in the program. The binary representation of the numbers, the bitwise NOT operation on them and the result are shown in the c program as comments. The program is as follows:

#include<stdio.h>
void main(){

    int a=14,b=4;
    
   /* a: 14 : 1110
      b:  4 : 0100
     ~a:   :  0001  
     ~b:   :  1011  
   */
   a=b-~a-1;
    /* b:   0100 -
      ~a:   0001
            0011 -
       1:   0001
            0010
     now a= 0010 
    */
    b=a+~b+1;
    /* a:   0010 +
      ~b:   1011
       1:   0001
    now b:  1110  (14)
       ~b:  0001
    */
    a=a+~b+1;
   /*  a:   0010 +
      ~b:   0001
       1:   0001
    now a:  0100 (4)
   */

    printf("a= %d\nb=  %d",a,b);
}

Spiral Matrix Program in C - Zoho Interview Question - C Program to Read a Matrix in Spiral Order

A spiral matrix is a matrix (two dimensional array) in which the elements are stored in spiral manner. The order of accessing elements in the spiral matrix resembles a spiral. Actually it is a normal NxN two dimensional array. But instead of accessing elements row-wise or column-wise, we access them in spiral order. i.e, in a 3x3 matrix the order of retrieval of elements is spiral order is as follows:

(0,0)  (0,1)   (0,2)  (1,2)   (2,2)   (2,1)   (2,0)   (1,0)   (1,1)

The following picture shows a 4x4 spiral matrix:
4x4 spiral matrix using two dimensional array - C Program for spiral matrix
A 4x4 Spiral matrix

Zoho, an IT company, asked an interview question which was to write a program to read a matrix spirally. In this post, we will answer this interview question  by zoho corporation. The only difference between spiral matrix and normal matrix is the order in which we store elements to matrix or access elements in matrix. The following is the C program to read a spiral matrix.

#include<stdio.h>
void main()
{
int a[10][10],o,cellcount,cc=0,c=0,i=0,j=0,g=1;
printf("\nEnter the order of spiral matrix: ");
scanf("%d",&o);
cellcount=o;
printf("\nEnter the elements:\n");
while(c<o*o)
    {
    cc=0;
    for(;cc<cellcount;j=j+g,cc++)
        scanf("%d",&a[i][j]);
    j=j-g;
    c+=cc;
    cc=0;
    i=i+g;
    cellcount--;
    if(c>=o*o)
        break;
    for(;cc<cellcount;i=i+g,cc++)
        scanf("%d",&a[i][j]);
    c+=cc;
    i=i-g;
    j=j-g;
    g*=-1;
    }
printf("\nThe spiral matrix is:");
for(i=0;i<o;i++)
    {
    printf("\n");
    for(j=0;j<o;j++)
        printf("%d\t",a[i][j]);
    }
}
The above program reads the matrix in spiral fashion and the displays the matrix in normal way. You can use this code not only to read and store the elements in matrix spirally but also to access the elements in the matrix in spiral order.

Related Posts:

C Compiler for Android Phones
C Program to Validate Sudoku - Check Correctness of Sudoku
C Program to Display Pascal's Triangle
C Program to Burst Consecutive Repetition of Numbers in Array
C Program to Display Strings Crossed
Tricky Interview Question - C Program to Rearrange an Array Without Using any Other Arrays
C Program to Convert Between Decimal, Binary, Hexadecimal and Octal Number Systems
C Program to Find Inverse of a Matrix
C Program to Find Largest Integer Formed Using Digits of Given Number
C Program to Count GrandChildren - Interview Question
How to get Random Number in C
C Program to Implement Circular Linked List
C Program to Swap Numbers with Bitwise Operators
C Program for Subtraction Without Using Minus Operator
C Program to Swap Numbers without Temporary Variable