Zoho Round 3-2
Zoho Round 3-2
Zoho Round 3-2
No: 1
Locker Problem
There is a school with 100 students, and correspondingly 100 lockers, all of which start off
closed. The first student opens every locker. The second student closes every other locker,
starting with the second (2, 4, 6 etc). The third student changes the state of every third locker
starting with the third (3,6,9 etc). The fourth would change the status of lockers numbered
4,8,12 etc.,. That is, if the locker is open, it is closed, and if it is closed, it is opened. This
continues until all 100 students have passed along the lockers. After the 100th student is done,
which lockers are open and which are closed?
[Note: program should work for any number of students/lockers]
Sample Input 1
100
Sample Output 1
open = 10
close = 90
Solution:
#include<stdio.h>
int main()
{
long long int N , square , ind = 0 , count = 0;
scanf("%lld" , &N);
square = 1;
ind = 2;
while(square <= N)
{
count++;
square = ind * ind;
ind++;
}
printf("open = %lld\nclose = %lld" , count , N-count);
return 0;
}
Test Case:
9 88 open = 9 Easy
close = 79
Q.No: 2
Find the Path
Given an (m x n) matrix, write a program to traverse the cell and print the values
present in the given path. Inclued necessary validation and proper error messages in
case of given path is out of bounds.
5 x 5 matrix :
{1 2 3 4 5 } (row 1)
{6 7 8 9 0 } (row 2)
{1 2 3 4 5 } (row 3)
{6 7 8 9 0 } (row 4)
{1 2 3 4 5 } (row 5)
Path Notation : ‘’>” is going right, “v” going down, “<” is going left, “^” is going up.
Example Input 1 :
Start at (Row, Column): 1, 2
Path: >>> v
Output: 2 3 4 5 0
Example Input 2:
Start at (Row, Column): 2,3
Path: v > > v < < ^ > > v v
Output 8 3 4 5 0 9 8 3 4 5 0 5
Example Input 3:
Start at(row, Column): 1 , 4
Path: > v > >
Sample Output 1
834509834505
Solution:
#include<stdio.h>
#include<malloc.h>
#define isBoundC(col) (col >= 0 && col < M)
#define isBoundR(row) (row >= 0 && row < N)
int main()
{
int *path , count = 0;
int N , len , M , row , col , flag,startrow , startcol , ind;
char str[100];
scanf("%d%d" ,&N,&M);
int arr[N][M];
for(row = 0 ; row < N ; row++)
{
for(col = 0 ; col < M ; col++)
scanf("%d" , &arr[row][col]);
}
scanf("%d%d",&startrow ,&startcol);
scanf("%s", str);
for(len = 0 ; str[len] ; len++);
path = (int*)calloc(len+1 , sizeof(int));
if(isBoundR(startrow-1) && isBoundC(startcol-1))
{
startrow--;
startcol--;
path[count++] = arr[startrow][startcol];
if(flag == 1 || count == 0)
printf("Invalid Path");
else
{
for(ind = 0 ; ind < count ; ind++)
printf("%d ", path[ind]);
}
}
return 0;
}
Test CAses:
Count Input Output Difficulty
1 55 834509834505 Sample
12345
67890
12345
67890
12345
23
v>>v<<^>>vv
2 34 1237323 Hard
1234
5678
1230
11
>>vv<>
4 56 32123904 Hard
123456
789012
123456
789012
123456
33
<<>>^>v
5 17 123456 Easy
1234567
11
>>>>>
6 55 432778787 Hard
12345
67890
67890
12345
22222
14
<<vv><><
8 10 10 876545656565474545 HArd
12345678 90 47478745654747
0987654321
12345678 90
0987654321
12345678 90
0987654321
12345678 90
0987654321
12345678 90
0987654321
23
>>>><<vv^^>>vv<><>vv
^><^<<>>vv^
10 33 123123123132323232 Hard
123 32323
321
123
11
>>v<<v>>^^<><><><><>
<>
Q.No: 3
Group anagram words
Given array of words, group the anagrams and print. Any word or phrase that exactly
reproduces the letters in another order is an anagram. Arrive most efficient algorithm.
Examples :
Input: {tar,rat,banana,atr,nanaba}
Output: Anagrams:
rat atr tar
nanaba banana
#include<stdio.h>
#include<malloc.h>
int strLen(char *str)
{
int ind;
for(ind = 0 ; str[ind ] ; ind++);
return ind;
}
int main()
{
char str[100][100];
int N , i , j , set = 0 , ind , ind1 , len , len1 , sum;
int *there;
scanf("%d" , &N);
int result[N] , count = 0;
for(ind = 0 ; ind < N ; ind++)
scanf("%s" , str[ind]);
}
if(set ==1 && str[ind][0])
{
printf("%s", str[ind]);
printf("\n");
}
else if(str[ind][0])
result[count++] = ind;
}
for(i = 0 ; i < count ; i++)
printf("%s\n" , str[result[i]]);
return 0;
}
Test Cases:
Count Input Output Difficulty
4 7 hjfdjf Hard
hjfdjf hfdjhfd jhfjfg hjfdf hfdjhfd
hjdfjdf hfd hjfd jhfjfg
hjfdf
hjdfjdf
hfd
hjfd
6 4 abc Easy
abc fhf jdfjg jd fhf
jdfjg
jd
8 3 qscdxrjmowfrxsjybldbefsar Hard
nwlrbbmqbhcdarzowkkyhidd cnwlrbbmqbhcdarzowkkyhi
qscdxrjmowfrxsjybldbefsarc dd
qscdxrjmowfrxsjybldbefsarcn nwlrbbmqbhcdarzowkkyhid
wlrbbmqbhcdarzowkkyhidd dqscdxrjmowfrxsjybldbefsa
hjfjdfhj rc
hjfjdfhj
9 5 wfrxsjybldbefsarcbynecdyg Hard
nwlrbbmqbhcdarzowkkyhidd gxxpklorellnmpapqfwkhonw
qscdxrjmowfrxsjybldbefsarcb lrbbmqbhcdarzowkkyhiddq
ynecdyggxxpklorellnmpapqf scdxrjmo
wkho nwlrbbmqbhcdarzowkkyhid
wfrxsjybldbefsarcbynecdygg dqscdxrjmowfrxsjybldbefsa
xxpklorellnmpapqfwkhonwlrb rcbynecdyggxxpklorellnmp
bmqbhcdarzowkkyhiddqscdx apqfwkho
rjmo ddqscdxrjmowfrxsjybldbefs
nwlrbbmqbhcdarzowkkyhidd arcnwlrbbmqbhcdarzowkky
qscdxrjmowfrxsjybldbefsarc hi
ddqscdxrjmowfrxsjybldbefsar nwlrbbmqbhcdarzowkkyhid
cnwlrbbmqbhcdarzowkkyhi dqscdxrjmowfrxsjybldbefsa
jdfjhfjkg rc
jdfjhfjkg
Q.No: 4
Permutation of string
A permutation, also called an “arrangement number” or “order,” is a rearrangement of the
elements of an ordered list S into a one-to-one correspondence with S itself. A string of length
n has n! permutation.
Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB
Sample Input 1
ABC
Sample Output 1
ABC ACB BAC BCA CBA CAB
Sample Input 2
1234
Sample Output 2
1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2
Solution:
#include<stdio.h>
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permutation(char *str, int l, int r)
{
int i;
if (l == r)
printf("%s ", str);
else
{
for (i = l; i <= r; i++)
{
swap((str+l), (str+i));
permutation(str, l+1, r);
swap((str+l), (str+i)); //backtrack
}
}
}
int main()
{
char str[100];
int len;
scanf("%s" , str);
for(len = 0 ; str[len] ; len++);
permutation(str, 0, len-1);
return 0;
}
Test Cases:
Q.No: 5
Solution:
#include<stdio.h>
char * strReverse(char *str)
{
int start , end ;
char temp;
for(end = 0 ; str[end] ; end++);
for(start = 0 , end-- ; start < end ; start++ , end--)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
}
return str;
}
int substring(char *s1 , char *s2)
{
Test Cases:
2 one two three four five one two five four three Hard
ree
5 hai hello how are you hai hai hello hai you are how hello Hard
hello
ello
8 one one one one two four three two one one one one Hard
three four
ne