Zoho 2nd and 3rd Round Coding Questions
Zoho 2nd and 3rd Round Coding Questions
1.Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that
finds if wildcard pattern is matched with text. The matching should cover the entire text (not
partial text).
The wildcard pattern can include the characters ‘?’ and ‘*’
‘?’ – matches any single character
‘*’ – Matches any sequence of characters (including the empty sequence)
2. Given an input string and a dictionary of words, find out if the input string can be
segmented into a space-separated sequence of dictionary words. See following examples
for more details.
Input: ilike
Output: Yes
Input: ilikesamsung
Output: Yes
3 2
6 5 4
10 9 8 7
10 9 8 7
6 5 4
3 2
1
4.Given an array as input, The condition is if the number is repeated you must add the
number and put the next index value to 0. If the number is 0 print it at the last.
Eg: arr[] = { 0, 2, 2, 2, 0, 6, 6, 0, 8}
Output: 4 2 12 8 0 0 0 0 0 .
6.Two sorted arrays will be given. Create an array consisting of the elements of two arrays
with duplicate elements removed in sorted order.
Note: Use only one loop. No sorting.
7. Two strings of equal length will be given. Print all the adjacent pairs which are not
equal.
Input: asdfghij and adsfgijh
Output: sd-ds, hij-ijh
9.From the input sentence given, find the strings which are not palindrome and print it.
Input: he knows malayalam
Output: he knows
10. Given two Strings s1 and s2, remove all the characters from s1 which is present in s2.
Input: s1=”expErIence”, s2=”En”
output: s1=”exprIece”
11. Find the next greater element for each element in given array.
input: array[]={6, 3, 9, 10, 8, 2, 1, 15, 7};
output: {7, 5, 10, 15, 9, 3, 2, _, 8} If we are solving this question using sorting, we need to
use any O(nlogn) sorting algorithm
12. Print all distinct permutations of a given string with duplicate characters.
https://www.geeksforgeeks.org/distinct-permutations-string-set-2
13. Given a number, find the next smallest palindrome
https://www.geeksforgeeks.org/given-a-number-find-next-smallest-palindrome-larger-than-
this-number
14.Given an array with repeated numbers, Find the top three repeated numbers.
input: array[]={3, 4, 2, 3, 16, 3, 15, 16, 15, 15, 16, 2, 3}
output: 3, 16, 15
15. 1.Given two dimensional matrix of integer and print the rectangle can be formed using
given indices and also find the sum of the elements in the rectangle
Input: mat[M][N] = {{1, 2, 3, 4, 6}, {5, 3, 8, 1, 2}, {4, 6, 7, 5, 5}, {2, 4, 8, 9, 4} };
index = (2, 0) and (3, 4)
Output:
Rectangle
46755
24894
sum 54
16. Find the result subtraction, multiplication, division of two integers using + operator.
Input: 6 and 4
output:
addition 6+4 = 10, subtraction 6+(-4) = 2, multiplication = 24, division = 1
Input : -8 and -4
Output:
addition -8+(-4) = -12, subtraction (-8)+(-(-4)) = -4, multiplication = 32, division = 2
17. Given a sentence of string, in that remove the palindrome words and print the
remaining.
Input:
He did a good deed
Output:
He good
Input:
Hari speaks malayalam
Output:
Hari speaks
18. Given two dates, find total number of days between them.
Input: dt1 = {10, 2, 2014} dt2 = {10, 3, 2015}
Output: 393
dt1 represents “10-Feb-2014” and dt2 represents “10-Mar-2015” The difference is 365 +
28
Input: dt1 = {10, 2, 2000} dt2 = {10, 3, 2000}
Output: 29
Note that 2000 is a leap year
Input: dt1 = {10, 2, 2000} dt2 = {10, 2, 2000}
Output: 0
Both dates are same
Input: dt1 = {1, 2, 2000}; dt2 = {1, 2, 2004};
Output: 1461
Number of days is 365*4 + 1
19. Let 1 represent ‘A’, 2 represents ‘B’, etc. Given a digit sequence, count the number of
possible decodings of the given digit sequence.
Examples:
Input: digits[] = “121”
Output: 3 // The possible decodings are “ABA”, “AU”, “LA”
Input: digits[] = “1234” Output: 3
// The possible decodings are “ABCD”, “LCD”, “AWD”
https://www.geeksforgeeks.org/convert-sentence-equivalent-mobile-numeric-keypad-
sequence/
Ex I/p abcccccbba
O/p 8 (from a to a)
I/p aaaaaaaa
O/p 6
21.sort the array odd numbers in ascending and even numbers in descending.
I/p 5 8 11 6 2 1 7
O/p 1 5 7 11 8 6 2
22. It’s about anagram.i/p was array of strings .and a word was given to find whether it
has anagram in given array.
I/p catch, got, tiger, mat, eat, Pat, tap, tea
Word: ate
O/p eat, tea
23. array of numbers were given to find a number which has same sum of numbers in it’s
either side.
I/p 1, 2, 3, 7, 6
O/p 7(has 1+ 2+3 in left 6 in right)
28. prime factor – sort the array based on the minimum factor they have.
29. adding a digit to all the digits of a number eg digit=4, number = 2875, o/p= 612119
30. Form the largest possible number using the array of numbers.
31. given a set of numbers, and a digit in each iteration, if the digit exists in any of the numbers, remove
its occurrences and ask for the next digit till the list becomes empty.
[ 10, 5, 30, 20 ]
Input : [ -1, 0, 3, 2 ]
[ 3, 4, 0, -1, 2 ]
34.Find the least prime number that can be added with first array element that makes
them divisible by second array elements at respective index (check for prime numbers
under 1000, if exist return -1 as answer) & (Consider 1 as prime number)
Input : [ 20, 7 ]
[ 11, 5 ]
Output : [ 1, 3 ]
Explanation :
(20 + ?) % 11
( 7 + ?) % 5
35. Sort the array elements in descending order according to their frequency of occurrence
Input : [ 2 2 3 4 5 12 2 3 3 3 12 ]
Output : 3 3 3 3 2 2 2 12 12 4 5
Explanation : 3 occurred 4 times, 2 occurred 3 times, 12 occurred 2 times, 4 occurred 1
time, 5 occurred 1 time
Input : [ 0 -1 2 1 0 ]
Output : 0 0 -1 1 2
Input: 1 1 2 3 1 2 4
Output: 2
Explanation: 1 occurs 3 times, 2 occurs 2 times, 3 occurs 1 time and 4 occurs 1 time.
Hence second frequently occurring number in given series is 2
Input: 2 10 4 8
Output: 2 8
Input: 1 10 6 8 13 21
Output: 1 8 13 21
Example:
Input: 1
Output: 0
Input: 2
Output:
0 0
0 1
1 0
1 1
Input: 3
Output:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Input:
N=4
1 1 0 0
1 0 0 1
1 1 1 1
0 0 0 1
Output:
_ 1 0 0
_ 0 0 1
_ _ _ _
0 0 0 _
Input:
Number of bits: 12
Bits: 1 0 1 1 0 1 1 0 1 1 1 1
Consecutive K: 2
Output:
1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0
42. Evaluate given expression which has factorials and exponential terms.
43. To implement snake and ladder game for given two-dimensional array having position
of snakes and ladders
44. To calculate strength of the password string using some predefined rules given in the
question
45. Given four points, We have to say whether it is square or rectangle or any other shape
50. Given an input array, find the number of occurrences of a particular number without
looping (use hashing)
Diamond pattern printing based on some conditions
Given an array of characters print the characters that have ‘n’ number of occurrences. If a
character appears consecutively it is counted as 1 occurrence
Eg: a b a a b c c d e d
Here a has only 2 occurrences
51. Efficiently merging two sorted arrays with O(1) extra space
https://www.geeksforgeeks.org/efficiently-merging-two-sorted-arrays-with-o1-extra-space/
53. Print the total number of odd and even digits in the given number.
Output : ODD 4
EVEN 3
Ex. INPUT :
Size of Array : 8
OUTPUT :
7
Ex. INPUT :
Size of Array : 4
OUTPUT :
Ex. INPUT :
Size of Array : 1
OUTPUT :
No second maximum
Ex. INPUT : 5
OUTPUT :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Ex. INPUT : 7
OUTPUT :
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
56. Given a two dimensional array which consists of only 0’s and 1’s. Print the matrix
without duplication.
Ex. INPUT :
1 0 1
1 1 0
1 1 1
1 0 1
OUTPUT :
Unique Matrix :
1 0 1
1 1 0
1 1 1
57. Given an array of positive numbers. Print the numbers which have longest continuous
range.
Ex. INPUT :
OUTPUT :
1 2 3 4
Ex. INPUT :
OUTPUT :
1 2 3 4
6 7 8 9
Input :
OUTPUT :
1 2 3 4 5 7
INPUT :
OUTPUT :
1 2 4
60. Given an array of numbers and a number k. Print the maximum possible k digit number
which can be formed using given numbers.
INPUT :
OUTPUT :
974
INPUT :
OUTPUT :
98973
61. Given an array of numbers and a window of size k. Print the maximum of numbers
inside the window for each step as the window moves from the beginning of the array.
INPUT :
OUTPUT :
5 5 5 8 8 9
62. Find the minimum number of times required to represent a number as sum of squares.
Input: 12
Output: min: 3
63. Search a string in a given 2D matrix. And print its possible path.
allowed movements are right left up and down.
Input: 0 1 1 0
1 1 1 0
0 0 1 1
0 0 1 1
Output: 2.
66. There are n items each with a value and weight. A sack is filled with the weights. In
other words there is an array with of length n having the values of the items arr[0…n-1] and
another array with weight arr[0…n-1].
if a sack is to be filled with weight W find the minimum possible value subset.
67. Write a program to determine whether a given number can be expressed as sum of
two prime numbers or not.
For example 34 can be expressed as sum of two prime numbers but 23 cannot be.
68. Take a 2 or 3 digit input number, reverse it and add it to the original number until the
obtained number is a palindrome or 5 iterations are completed.
Input : n = 32
Output : 55
23 + 32 = 55 which is a palindrome.
Input : 39
Output : 363
69. Given a string, reverse only vowels in it; leaving rest of the string as it is.
Input : abcdef
Output : ebcdaf
70. Write a program to check if the given words are present in matrix given below. The
words can be left to right, top to bottom and the diagonals (in top to bottom direction)
71. Write a program to form lines using given set of words. The line formation should
follow below rules.
i) Total characters in a single line excluding the space between the words and the favorite
character should not exceed the given number.
ii) Favorite character is case insensitive.
iii) Words should not be broken up. Complete words alone should be used in a single line.
A word should be used in one line only.
Loving, Mango
Eating Mango
Loving Pogo.
72. Adding 2 numbers
GIven 2 huge numbers as seperate digits, store them in array
and process them and calculate the sum of 2 numbers and store
the result in an array and print the sum.
Input:
Number of digits:12
9 2 8 1 3 5 6 7 3 1 1 6
Number of digits:9
7 8 4 6 2 1 9 9 7
Output :
928920295113
78. Given an odd length word which should be printed from the middle of the word.
The output should be in the following pattern.
Example:
Input: PROGRAM
Output:
GR
GRA
GRAM
GRAMP
GRAMPR
GRAMPRO
79. It is a program to implement Least Recently Used (LRU) concept. Given a key, if it is
already existed then it should be marked as recently used otherwise a value should be stored
which is given as input and marked as recently used. The capacity is to store only 10 key,
value pairs. If the table is full and given a new key; the key, value pair which is not recently
used should be deleted which gives feasibility to store the new key, value pair.
80. Given a few pairs of names in the order child, father. The input is a person name and
level number. The output should be the number of children in that particular level for the
person given.
Example:
Input:
[
{Ram, Syam},
{Akil, Syam},
{Nikil, Ram},
{Subhash, Ram},
{Karthik, Akil}
];
Syam 2
Output: 3 (Syam has Ram and Akil in level 1 and in level 2 he have Nikil, Subhash, Karthik.
So the answer is 3).
81. Given an array of positive integers. The output should be the number of occurrences of
each number.
Example:
Input: {2, 3, 2, 6, 1, 6, 2}
Output:
1–1
2–3
3–1
6–2
82. sort numbers based on digits starting from most significant numbers. Eg: input-100 1 11
21 2 3. Output-1 100 11 2 21 3
83. Given an array, find the minimum of all the greater numbers for each element in the
array.
Sample:
Output:
84. Find the largest sum contiguous subarray which should not have negative numbers. We
have to print the sum and the corresponding array elements which brought up the
sum.
Sample:
Array : {2, 7, 5, 1, 3, 2, 9, 7}
Output:
Sum : 14
Elements : 3, 2, 9
85. Given a string, we have to reverse the string without changing the position of
punctuations and spaces.
86. Given a 2D grid of characters, you have to search for all the words in a dictionary by
moving only along two directions, either right or down. Print the word if it occurs.
Sample :
a z o l
n x h o
v y i v
o r s e
Output:
zoho
love
Is
87. Given a string, change the order of words in the string (last string should come first).
Should use RECURSION
90. Write a program to print all permutations of a given string. Note here you need to take
all combinations as well, say for the input ABC the output should be as follows:
Input: ABC
Output:
A
B C
AB AC BA BC CA CB
ABC ACB BCA BAC CBA CAB
92. https://www.geeksforgeeks.org/reverse-words-in-a-given-string/
Input Output
72581 7(2+5)81
77(8-1)
777
3962 cannot create a mono digit number
94. You’re given an array. Print the elements of the array which are greater than its previous elements in
the array.
Input : 2, -3, -4, 5, 9, 7, 8 Output: 2 5 9 You should solve this question in O(n) time.
95.
You’re given an even number n. If n=4, you have to print the following pattern :
4444
4334
4334
4444
96. You’re given a number n. If write all the numbers from 1 to n in a paper, we have to find the number of
characters written on the paper.For example if n=13, the output should be 18 if n = 101, the output should
be 195
97. A number is called as binary-decimal if all the digits in the number should be either ‘1’ or
‘0’. Any number can be written as a sum of binary-decimals. Our task is to find the minimum
number of binary-decimals to represent a number.Input : 32Output : 10 11 11
Input : 120
Output : 10 110
98. You’re given a string as an input. You have to reverse the string by keeping the
punctuation and spaces. You have to modify the source string itself with creating an another
string.
Input :A man, in the boat says : I see 1-2-3 in the sky
Output :
y kse, ht ni3 21ee slsy : a sta o-b-e ht nin amA
99. Given two numbers a and b both < 200 we have to find the square numbers which lie
between a and b(inclusive)
o/p 25,36,49,64,81,100
o/p {9,2,8,3,7,4,5}
o/p count = 17
explanation:
5 {3,2} 2
8 {3,3,2} 3
10 {3,3,3,1} 4
13 {3,3,3,3,1} 5
6 {3,3} 2
2 {2} 1
101. Given two binary numbers add the two numbers in binary form without converting
them to decimal value.
o/p 100011
eg) a = 123 b = 13 n = 4
o/p 202
102. Write a program to print the below pattern
for n = 6
1 7 12 16 19 21
2 8 13 17 20
3 9 14 18
4 10 15
5 11
103. Given bigger NxN matrix and a smaller MxM matrix print TRUE if the smaller matrix can
be found in the bigger matrix else print FALSE
104. Given two matrices a and b both of size NxN find if matrix a can be transformed to
matrix b by rotating it 90deg , 180deg , 270deg if so print TRUE else print FALSE
105. In addition to the above question you have to check if matrix a can be transformed by
mirroring vertically or horizontally to matrix b.
106. Given an array of integers, rearrange the array in such a way that the first element
is first maximum and second element is first minimum.
Output : {7, 1, 6, 2, 5, 3, 4}
Output : ((abc)(de))
Input : (((ab)
Output : (ab)
108. Form a number system with only 3 and 4. Find the nth number of the number system.
Eg.) The numbers are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333,
3334, 3343, 3344, 3433, 3434, 3443, 3444 ….
Output : Valid
Input : (ab)(ab+)
Output : Invalid
Input : ((a+b)
Output : Invalid
111. Given two sorted arrays, merge them such that the elements are not repeated
Eg 1: Input:
Array 1: 2,4,5,6,7,9,10,13
Array 2: 2,3,4,5,6,7,8,9,11,15
Output:
Eg 1:Input:
String 1: test123string
String 2: 123
Output: 4
Eg 2: Input:
String 1: testing12
String 2: 1234
Output: -1
113. Write a program to print the following output for the given input. You can assume the
string is of odd length
Eg 1: Input: 12345
Output:
1 5
2 4
2 4
1 5
Eg 2: Input: geeksforgeeks
Output:
g s
e k
e e
k e
s g
f r
f r
s g
k e
e e
e k
g s
114. Write a program to sort the elements in odd positions in descending order and
elements in ascending order
Output: 13,2,12,10,5,15,4
Eg 2: Input: 1,2,3,4,5,6,7,8,9
Output: 9,2,7,4,5,6,3,8,1
115. Write a program to give the following output for the given input
Eg 1: Input: a1b10
Output: abbbbbbbbbb
Output: bbbccccccddddddddddddddd
1.
4.Size of the array is given were w is wall, g ground, o ball, numbers are bricks.
I/p size 7
Number of bricks 6
Position (2, 2)(2, 3)(2, 4)(3, 2)(3, 3)(3, 4)
Ball:5(life)
wwwwwww
w w
w 1 1 1 w
w 1 1 1 w
w w
w w
w g g o g g w
There are three commands St, lt, rt straight, left, right respectively.
If it is st the ball moves straight .if there any brick(1) along the way it hit it .then brick
disappear.ball back to original position.if there is no brick .it come to initial position.
I/p st
O/p
wwwwwww
w w
w 1 1 1 w
w 1 1 w
w w
w w
w g g o g g w
Ball count:5
I/p lt
O/p
wwwwwww
w w
w 1 1 1 w
w 1 w
w w
w w
w g o g g g w
Ball count:4
(Lt : ball moves diagonally left there is no bricK on the way so it hit the wall reflect back
horizontally there is a brick(3, 2) after hitting it it moves downwards ball position
changed.hence ball count get reduced. On moving downwards is there any brick that also
disappear.)
Same for rt but moves diagonally right.
This is first module.In second module each brick has value for each hit value get
reduced.it disappear only when it become zero.
There are about 7 modules.
5. implement a game in C, that’s well known as ‘Tetris blocks’. It involves full of matrix
operations.
6. This time all the questions were based on matrix. I got 6 programs, again you get
the next program after you complete the current program. All the programs were just
an addition to the previous one. Here the concepts to be strong about were logic for
diagonals ( all the four, top-left, top-right, bottom-left, bottom-right), finding the path
in a matrix from source to destination.
7. Lift system
There were 8 modules
1. Display the position of Lift
Lift : L1 L2 L3 L4 L5
Floor: 0 0 0 0 0
Input : 2 5
Output : L1 is assigned
Lift : L1 L2 L3 L4 L5
Floor: 5 0 0 0 0
Lift : L1 L2 L3 L4 L5
Floor: 5 2 7 9 0
Input : 4 10
Output :
L1 is assigned
Lift : L1 L2 L3 L4 L5
Floor: 10 2 7 9 0
10. Given a MxN matrix filled with ‘-‘ and you need to drop the balloons in the desired
columns starting from the bottom. You need to print the matrix when a new balloon
is dropped.
You need to continue getting inputs until the box is full or until the user chooses to
stop.
TEST CASE :
- - -
- - -
- R -
- - -
- B -
- R -
- - -
- B -
R R -
- R -
- B -
R R -
Program Stopped
Extended version of the previous problem. Now you need to quit when a row become filled completely.
TEST CASE :
- - -
- - -
- R -
- - -
- B -
- R -
- R -
- B -
- R -
Extended version of the previous problem. Now you need to drop balloon in the first free cell from left if the
specified column is filled in every row.
TEST CASE :
- - -
- - -
- R -
- - -
- - -
B R -
- - -
- - -
B R R
- - -
- R -
B R R
- - -
B R -
B R R
Program terminated.
Extended version of the previous problem. If any column has three continuous balloons of same colors then
we need to burst them.
TEST CASE :
- - -
- - -
- R -
Do you wish to continue(Y/N) : Y
- - -
- - -
R R -
- - -
- - -
R R R
- - -
- R -
R R R
- - -
R R -
R R R
- - -
R R R
R R R
- - -
R - R
R - R
Program Terminated.
Extended version of the previous problem. Now you need to burst the three continuous colors in the same
row.
They insisted us to do it in a object oriented language. First they asked the design( what are all
the classes and objects & what data structure do you use).
Application description:
There are ‘n’ number of points in a highway out of which some points collect toll.
Each toll has its own charging scheme according to the vehicles and whether or not they
are a VIP user.
If they are VIP user, 20% discount apply.
If the vehicle passes 3 toll gates, it has to pay in all the 3 toll gates according to the
scheme of respective tolls.
There were 4 modules.
1. Given the details of vehicle type, start and destination……display the total toll paid during
the journey and print the amount after applying the discount.
2. Display the details of all the tolls…..like what are all the vehicles(vehicle number) passed
that respective toll and the amount each vehicle paid……and the total amount charged in
that toll.
3. Display the details of all the vehicles …….like what are all the journeys did it take….the
start and destination of the same……tolls it passed during that journey….amount paid in
that journey…..and the total amount paid by that vehicle.
4. Assume the highway as a circular path……we have to find the short route and identify
the tolls between that route and calculate the amount.
14. We were asked to design an application program for n*n tic-tac-toe game. Here, you
are expected to code with proper standards and in a most optimized way. And, in this
round you need to find all the edge cases and corner cases yourself. The interviewers
won’t help you if you miss anything. So, make sure you covered all the cases before
showing output to the interviewers.
https://www.geeksforgeeks.org/implementation-of-tic-tac-toe-game/
17 . text editor
Only 40 characters per line and words should be wrapped if they brake
Also perform insert delete operations
struct product {
char productname[20];
int product_price;
int product_id;
Get the product name, price and id and display the product name and price in descending
of the price.
For the same above structure, now add another structure which is the category. That
category will have products in it.
Struct category
char category_name[20];
int cat_id;
According the category get the product name, product price and id, then display all the
products category wise in descending order.
For the same structure which as category and product, get the category id from the user
in the product structure and save to the category list. Then display them all in category
wise.
A sheet full of data will be given with inventory stock list, which as different categories
and different products as input with category capacity and product availability in the
structure. Now we need to add a new category or new product with capacity and
availability. Need to check whether the product availability is exceeding the category
capacity, if yes the output rack is full or else tell how much free space is available and
add the product to list.
Constraints in the above in question will be given, need to solve all the constraints
19. Design a Call taxi booking application
-There are n number of taxi’s. For simplicity, assume 4. But it should work for any number
of taxi’s.
-The are 6 points(A,B,C,D,E,F)
-All the points are in a straight line, and each point is 15kms away from the adjacent
points.
-It takes 60 mins to travel from one point to another
-Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the
subsequent kilometers.
-For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc.
-All taxi’s are initially stationed at A.
-When a customer books a Taxi, a free taxi at that point is allocated
-If no free taxi is available at that point, a free taxi at the nearest point is allocated.
-If two taxi’s are free at the same point, one with lower earning is allocated
-Note that the taxi only charges the customer from the pickup point to the drop point. Not
the distance it travels from an adjacent point to pickup the customer.
-If no taxi is free at that time, booking is rejected
Design modules for
Input 1:
Customer ID: 1
Pickup Point: A
Drop Point: B
Pickup Time: 9
Output 1:
Taxi-1 is allotted
Input 2:
Customer ID: 2
Pickup Point: B
Drop Point: D
Pickup Time: 9
Output 1:
Taxi-2 is allotted
(Note: Since Taxi-1 would have completed its journey when second booking is done, so
Taxi-2 from nearest point A which is free is allocated)
Input 3:
Customer ID: 3
Pickup Point: B
Drop Point: C
Pickup Time: 12
Output 1:
Taxi-1 is allotted
Output:
1 1 A B 9 10 200
3 3 B C 12 13 200
2 2 B D 9 11 350
These were just sample inputs. It should work for any input that they give.
Those who finished both the modules within 3 hours and if it worked for all the inputs they
give, those candidates were given extra modules to work with.
20. A matrix game was given with 5 rules. We were asked to implement each of the rules
separately.
R3 | - - - |
R2 | - - - |
R1 | - - - |
C1 C2 C3
Each of the 9 cells can either be empty or filled with an atom. R3, R2, R1 are the rays
that originate from the left. C1, C2, C3 are the rays that originate from the bottom of the
box.
Input : Position of the atoms and the rays that gets originated from the outside of the box.
Eg.) 3
3 1
2 2
1 3
R3 C1 C3
Rule 1:
A ray that has an atom in its path should print ‘H’ (Hit) If it does not have any atoms in its
path, the ray should pass to the other side.
C1 C3
R3 | - - - | R3
H | - X - |
R1 | - - - | R1
C1 H C3
Rule 2 & 3:
A ray that has an atom in its diagonal adjacent position should refract.
H | - - - |
H | X - - |
R | - X - |
R H R
H | - X - |
R2 | - - - | C3
| - - - |
R2 C3
Rule 4:
A ray that has atoms in both of the diagonal adjacent positions should reflect back.
Input ray: C2
| - - - |
| X - X |
| - - - |
Input ray: R2
| - X - |
R | - - - |
| - X - |
Rule 5:
The deflection of rays should happen in the order of the input rays.
H | - X - |
R2 | - - - | C3
| - - - |
R2 C3
The final task was to implement these rules for dynamic matrix size.
Input : no of rows, no of columns
2 (No of atoms)
4 4 (Position of atom)
2 2 (Position of atom)
2 (No of rays)
R4 C2 (Ray number)
H | - - - X |
| - - - - |
| - X - - |
| - - - - |
The final task was very confusing and it had to handle all the cases. There are chances
for a ray to end at the starting position if the number of rows and columns are more than
5.