Mockcodes Questions
Mockcodes Questions
SequenceDetector
Problem
Nitya is a digital circuit designer. She decides to design a sequence detector which may
be either an overlapping model or a non-overlapping model.
Sequence detector is a digital circuit which is used to detect the sequences from a
series of numbers. These detectors may be overlapping or non-overlapping.
So, from the series given above, say Series 1, non-overlapping sequence detector
detects four 101 sequences
From the series given above, say Series 2, overlapping sequence detector detects
three 101 sequences. The last 5 digits in the given series i.e., 10101 has
two 101 sequences where the middle 1 is used by both left and right trailing digits.
The process of any circuit design starts with requirements and state transition diagram
to represent that requirement. So, Nitya asked her assistant to create di erent state
transition diagrams for the hardware needed to detect di erent sequences. Hardware
will need to be customized per sequence. The assistant designed di erent hardware for
di erent sequences, but forgot to mention the sequence to be detected on respective
state transition diagrams. So now, Nitya must go through every sequence and
determine, what is the sequence to be detected and whether the sequence detector is
overlapping or non-overlapping.
So, she decides to code the state transition diagram and find which sequence is to be
detected and what is the type of detector.
For example,
Here state changes from a to b when input is 1 and giving output 0. So, the sequence
starts with 1. State changes from b to c when input is 0. So, the second digit in
sequence is 0. State changes from c to a when input is 1 and giving output as 1, and also
when input is 0 and giving output as 0. But the output is 1 only when input is 1 from the
above diagram. So, the third digit in sequence is 1. Thus, the sequence that this state
transition diagram can detect is 101.
A sequence detector is non-overlapping when state changes from final state to initial
state with output being 1. In this case, initial state is a and final state is c. In any other
case, it is an overlapping sequence detector. A sequence is said to detected by
observing the output when the output goes to 1. So, the above state model is non-
overlapping sequence detector.
Constraints
Each input line contains 4 space separated inputs viz. < present_state (char), next_state
(char), input (0/1), output (0/1)>
For a sequence to be detected, every state must be visited at least once and the
number of transitions should be equal to Number of states
Input
Input consists of variable number of lines of input where each line contains 4 space
separated inputs which represents present_state (char), next_state (char), input (0/1),
output (0/1) of the state transition diagram.
Output
Print the type of sequence detector in the second line. If the detector is non-overlapping
print "Non Overlapping Sequence Detector" else print "Overlapping Sequence
Detector".
Examples
Input
ab10
bc00
bb10
aa00
ca00
ca11
Output
101
Explanation
Here a and b represent di erent states in state transition diagram. state of circuit
changes from one state to another depending on inputs provided by arrow
representation. Initial state starts with 'a' and then goes to 'b' and then to 'c' and so on. A
sequence can be formed only when state changes.
Here state changes from a to b when input is 1 and output is 0. So, the sequence starts
with 1. State changes from b to c when input is 0. So, the second digit in sequence is 0.
State changes from c to a when input is 1 and output is 1, and also when input is 0 and
output is 0. But the output is 1 only when input is 1 from the above diagram. So, the third
digit in sequence is 1. Thus, the sequence that this state model diagram can detect is
101
Since a sequence detector is non-overlapping when state changes from final state to
initial state with output being 1, the state transition diagram above is a non-overlapping
sequence detector, where, the initial state is a and final state is c.
101
Example 2
Input
ab10
aa00
ba00
bc10
cc10
da00
db11
cd00
Output
1101
Explanation
Using a.
bove state transition diagram, the sequence is 1101 and detector is overlapping
detector. Thus, the output is
1101
Given T binary strings of varying lengths which consists of only 0s and 1s. He will be
given two values A and B which indicates cost of one occurrence of sub strings "01" and
"10". The total cost of the given string will be the sum of the costs of all "01" and "10". His
task is to minimize the cost of given strings in each case, by rearranging it in any order.
After he rearrange the string, he has to find the hamming distance between the original
string and the rearranged string and print it in each case. In case of invalid input, print
"INVALID". If there are more than one rearrangement which gives least cost, then
consider the string which gives minimum hamming distance.
As Vyom is new to binary strings, he is a bit confused. Can you help Vyom to implement!
Note: The sub strings are considered in an overlapping manner i.e., in the string 010,
there is one "01" and one "10".
Constraints
Input
For each test case there will be two lines, first line consists of the binary string and the
second line consists of A and B separated by space.
Output
1
Examples
Example 1
Input
0100
32
000
45
Output
Explanation
Here, cost of original string viz. 0100 is 5, because there is one occurrence of both "01"
and "10". Now this string can be transformed into a new string viz. 1000 which is having
one occurrence of "10". The cost of transformed string= (number of occurrences of
"01")*3 + (number of occurrences of "10")*2 = 0*3 + 1*2 = 2 which is the minimum
possible and the hamming distance of original and transformed string 2.
The string 000 has the cost of 0 which is minimum, and hence no need to do any
transformation. So the hamming distance will be 0.
Example 2
Input
01001a10
12
Output
INVALID
Explanation
Each device can find where other devices are and how far they are. This information is
shared among devices, creating a network. This network helps with communication and
figuring out where devices are in relation to each other. To make the system more
accurate, all devices in the network sync up and point in the same direction.
In a real time scenario , each devices is able to communicate with each other, but it is
not able to find the direction and distances of all devices due to natural obstacles.
However for few devices it may be possible to know their direction and distances. Now
Your job is to find how far apart two specific devices are, in this connected network.
Each device has a unique ID for easy communication and identification.
The calculations required to find distance between devices can be understood from the
through the Examples section
Constraints
1 < N < 16
Distance is in meters
Input
First line consists of an Integer 'N' denoting total number of place finder devices
Second line consist of 'N' pairs of space separated integers. Each pair is separated by a
colon. First value of the pair represents the device ID, second value represents the
number of devices that can be found by this device.
Next lines consist of "N" logical sections. Each logical section consists of
Followed as many number of lines as devices that can be found by this device.
Each such lines has three space separated Integers denoting "Device ID", "
Distance" and "Angle".
Last line consists of two space separated integers representing Id of devices between
whom we have to find the distance .
Output
Print the Distance rounded o to two decimal points denoting the distance between the
given two devices
Examples
Input
290
3 8 270
5 8 330
4 8 270
1 9 180
1 8 90
490
2 8 90
3 9 180
5 8 30
2 8 150
4 8 210
15
Output
16.42
Explanation
The second line describes that the device "1" is able to find the direction and distance
of 2 devices and device "2" is able to find the direction and distance of 3 devices and so
on.
From second line of this logical section, device "2" is located at distance of 9 m from the
device "1" at the angle of 0 degree.
From third line of the logical section, device "3" is located at distance of 8 m from the
device "1" at an angle of 270 degree.
By continuing similarly for all the logical sections, we can visualize the network. Below
figure depicts the network
4. ScoreOfCells
Problem
James is a school going kid, who just learned about rows, columns and tables. On one
fine day, he drew a table consisting of n rows and m columns. In each cell, he wrote a
number randomly.
James defined a concept "score" for each cell. The score of a cell A is the number of
unique ways possible from all the cells to reach A. For this, he has to follow a rule which
states that, he can only move from current cell to either down/right cell only if the value
those cells hold is not lesser than the value in the current cell.
Given an integer k, find out the cells with score k. If there are more than one cell, then
print the indices of the cells in the order they occur in the table from left to right and top
to bottom. Print "NO" if there are no cells with such score.
Note: Two ways are said to be unique if they have at least one di erent cell in its path.
Constraints
Input
Output
Examples
Example 1
Input
43
645
853
972
1 9 10
Output
02
10
11
Explanation-
The score of a cell A is the number of unique ways possible from all the cells to reach A.
For the cell [0,2], [0,1] -> [0,2] is the only possible way.
For the cell [1,0], [0,0] -> [1,0] is the only possible way.
For the cell [1,1], [0,1] -> [1,1] is the only possible way.
So, the score of all these cells is 1 and we print them in the order of their occurrence.
Example 2
Input
33
6 16 19
14 20 17
21 12 11
Output
11
Explanation-
The score of a cell A is the number of unique ways possible from all the cells to reach A.
Now when we measure the distance between the device "1" and device "5" we get the
output as 16.42.
Example 2
Input
250
3 5 60
1 5 180
3 5 120
4 5 60
2 5 300
450
5 5 60
1 5 240
2 5 240
3 5 180
5 5 120
3 5 240
4 5 300
15
Output
10.00
Explanation
Above diagram depicts the network. The distance between the device "1" and device "5"
are 10.
Our observation revealed that the virus consistently targets the container with the
maximum data units. However, when two containers have equal data units, the virus's
behavior becomes unclear, and we must avoid such scenarios. To determine the data's
destination, continuous communication between the virus and the thief must occur.
However, our priority is to prevent further data theft.
As the virus targets containers with more data, it always seeks neighboring containers.
It can travel in all 8 directions in its quest for infecting container with maximum data. If
any of the 8 neighboring container has more data units, the virus moves there.
Consequently, our strategy is to entice the virus into a storage unit containing dummy
data. Also since data can be generated on the fly, assume that infinite amount of data
can be fudged (made available) to lead and trap the virus into desired location.
Information about the virus's current location and the desired location, which is the
dummy container, is known. Given our awareness of the data warehouse's size (M*N)
and the data stored in the containers, we aim to determine the minimum data required
to be produced to entice the virus to the dummy container.
Adding data consumes time, so identifying where to fudge how much data, is crucial.
The goal is to trap the virus with minimum amount of fudged data.
Constraints
2 <= N, M <= 7
Input
Next line consist of two space separated integers, indicating current location of the
virus
Last line consists of two space separated integers, specifying the location of the
dummy container where we want to entice the virus to.
Output
The smallest amount of overall data needed to be fudged to attract the virus to the
dummy container.
Examples
input
44
1234
5678
9 10 11 12
13 14 15 16
22
41
output
Explanation
The virus is on container [2,2] and we have to bring it to container [4, 1]. The
neighbouring containers of [2, 2] are [(1,1),(1,2),(1,3),(2,1),(2,3),(3,1),(3,2),(3,3)] having
data of [1,2,3,5,7,9,10,11] units, respectively. Now we know that virus will move to
container [3, 3] because it contains the maximum amount of data in its neighbourhood.
However, we also know that from [3, 3] reaching [4, 1] is impossible. Hence we now
need to fudge data.
We can fudge and add 3 units of data to [3,1] where already 9 units of data is present.
After fudging, it will became 12. 12 units of data will make it the maximum in
neighbourhood. The virus will now thus move to [3,1] .
Now the virus is on container [3,1], The neighbouring container of the virus are
[(2,1),(2,2),(3,2),(4,1),(4,2)] having data of [5,6,10,13,14] units, respectively. we will add
2 units of data to [4,1] where already 13 units of data is present. Adding 2 unit data will
make it 15. 15 units of data is the maximum in the neighbourhood. The virus will then,
move to [4,1], our dummy container
A total of 3+2 = 5 units of data was required to be fudged to entice the virus to desired
location.
Example 2
input
56
1 17 18 20 11 10
3 17 15 18 16 15
10 11 20 6 8 3
18 18 5 11 4 16
3 4 8 17 18 20
11
34
output
16
Explanation
We will add 1 unit of data to [2,2] so they virus will move there, Next we want to move it
to [3,3] but it is already the maximum in its neighbourhood. So, no new data needs to be
added. Next, 15 units of data is required to make [3,4] maximum. So 15 + 1 = 16.
6.HarmonicHomology
Problem
There are several tunes in the music world. When these tunes are combined, we get a
soothing melody. The combination of these melodies will result in a complete song.
Ananya is a musician who always tries to create unique songs. She mixes up these
tunes together and forms melodies. There is a standard hierarchy for these tunes which
resembles a tree. All the tunes that belong to a certain level sound alike, and the parent
of these tunes defines their category.
The hierarchy will be given in the input. Ananya is currently composing a song, to make
the song more musical, she will have to form melodies which sound similar.
You will be given the hierarchy of the tunes, two melodies, and three integers A, B and C.
A melody is the chain of tunes separated by "-". Two tunes are said to be similar if they
are equal or belong to the same level in the hierarchy. You are allowed to perform two
operations on tunes i.e., compare or remove. A term "concordance score" is introduced,
which is initially zero. When you compare, if tunes are similar, then concordance score
will be increased by A or else decreased by B. If you prefer to remove a tune from one of
the string, concordance score will be decreased by C. You cannot remove tune at ith
index from both the strings simultaneously. You will have to move to the next tune only
when the current tune is either compared or removed.
Ananya is trying to increase the similarity between the given melodies by increasing
their concordance score by performing the above-mentioned operations. Thus, the
more the concordance score, more the similarity!
Note: These terms used here do not resemble original music terms.
Constraints
Input
The first line consists of a single integer N denoting the number of parent nodes.
Next N lines contains music categories in the form of <parent tune> : <list of child tunes
separated by space>. The first parent tune is always the root node of the tree.
N+2 line consists of melody m1.
Output
Examples
Example 1
Input
country : swing
rock-swing-new-salsa-swing-blues
swing-swing-blues-jazz
421
Output
14
Explanation-
Remove rock and swing tunes from melody1. Melody1 becomes new-salsa-swing-
blues. Concordance Score = -2
Compare new tune from melody1 with swing tune from melody2. Both are similar.
Concordance Score = -2+4 = 2
Compare salsa tune from melody1 with swing tune from melody2. Both are similar
because both are at the same level. Concordance Score = 2 + 4 = 6
Compare swing tune from melody1 with blues tune from melody2. Both are similar
because both are at the same level. Concordance Score = 6 + 4 = 10
Compare blues tune from melody1 with jazz tune from melody2. Both are similar
because both are at the same level. Concordance Score = 10 + 4 = 14
No other set of operations will give a concordance score greater than 14.
Example 2
Input
brio : funk
briet : punk
funk : disco
punk-brio-funk-briet
disco-carol-heavy-brio
10 8 2
Output
12
Explanation-