Search This Blog

Showing posts with label zoho. Show all posts
Showing posts with label zoho. Show all posts

Array Rearrangement Without Another Array - Zoho Programming Round Question


Question:

You are given two integer arrays. The first one is a set of integers which you should rearrange and produce an output array based on the second array. The second array, we call it Order array, is an array which directs the rearrangement by specifying the required position of each element in given array. You should rearrange the elements just as specified by the order array without using any additional arrays. You should write a c program to rearrange the number array but without using another array. An example is as follows:

number array: 46   80  10   8    7    2   11   16  17  43
order array   :  5     2    0   6    8    7    1     3    4   9
output          :  2    10  46  11  17  16   80    8   7    43


Problem Explanation:

order[0]=5 means the current element at index 5 in the given number array should be at index 0 in the desired array. Similarly order[1]=2 means  the current element at index 2 in the given number array (10) should come at index 1 in the desired array.

Thus, the question is to write a c program to rearrange the given array without using another array to produce the desired array as specified by the order array.

This was one of the questions asked in a programming test conducted as a part of recruitment process of Zoho corporation, an IT firm. This is one of the tough c programming questions asked in second hands on coding round.

Before answering the question, you must know that it is possible to produce the desired output array without actually performing the rearrangement. It is not so difficult.The following code fragment can do it.

for(i=0;i<10;i++)
printf("%d  ",nums[order[i]]);

As emphasized in the question, the array should be rearranged, but your are not allowed to use any additional arrays.

Answer:

Actually, this can't be done without additional allocation of memory. But, to answer this question, we should avoid explicit use of additional arrays. So, we can make use of recursion. In fact, for each call of a recursive function, memory is allocated in stack for its local variables. We make use of recursion to exploit the stack. I would first write the program, then explain it.

#include<stdio.h>
int nums[]={ 46,80,10,8,7,2,11,16,17,43};
int order[]={5,2,0,6,8,7,1,3,4,9};
int i;
void rec(int index)
{
int num;
num=nums[order[index]];
if(index<9)
rec(index+1);
nums[index]=num;
}
void main()
{
printf("rearranging\n");
i=0;
rec(i);
printf("rearranged:\n");
for(i=0;i<10;i++)
printf("%d  ",nums[i]);
}


The parameter index in each recursion stores the index (position) and local variable num stores the desired number at that position. Thus value of parameter 'index' varies from 0 to 9 (here) and corresponding number at the index is also held in variable num. Thus, after the last call of recursive function, the whole array (index and the number at that index) get stored in stack. Only after this, the num is placed at its index.

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

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