0% found this document useful (0 votes)
186 views

Practice TCS NQT Advanced Coding - Questions Only

TCS and Accenture training

Uploaded by

ganesh balaji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Practice TCS NQT Advanced Coding - Questions Only

TCS and Accenture training

Uploaded by

ganesh balaji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

TCS NQT Advanced Coding Questions

Question 1) Alice and her friends are playing a game of verbal Kho-Kho. Alice is acting
as a mediator, and the rest of the N friends are seated on N chairs, one each. Alice starts
by providing a paper with a single-digit number to the friend present at number 1. Let’s
denote friends by F, where F will be of size N.

F[1]…F[N] represents friends seated respectively. After receiving the paper


with a digit, F[1] will enact and try to tell F[2] without speaking. Similarly, F[2] will
communicate to the next person i.e., F[3]. This continues until the last person F[N]
understands the digit. Finally, the last person will write the digit on a separate paper
and give it to Alice to compare both papers. If the digits are similar then, Alice will
give a T-shirt to each friend. However, if the digits do not match, Alice will ask each
friend’s digits, and she will offer the T-shirts to only those who understood the digits
correctly.

Given N number of friends and digit array D, denoting the digit understood by each
friend F, finds out how many of Alice’s friends have not enacted well OR did not
understand the enactment by the previous friend correctly.

Example 1:
3 -> N, number of friends
4 4 4 – array D. denoting digit understanding by N friends

Output:
0
Explanation:
All of them understood the digits correctly.
Example 2:
5
12322
Output:
4

Explanation:
1st, 2nd, 3rd, and 4th friends could not enact OR understand the enactment.

Question 2)
Bob is going to bet today on horse riding. There are N horses listed in a sequence of 1
to N.

The probability of winning each horse is different so the prices for making a bet on the
horses are not the same. There is no limit on the number of horses on which he can bet,
but he thinks that if he bets on a continuous sequence of horses then he has a better
chance to win. Bob will get K units of money if any horse on which he bets will win.
But as the award is only K units so he wants to put money less than K.

VelTech University / 2024 / Page 1 / 27


Bob wants to bet on as many horses as he can. As you are his best friend, he reached
out to you for help, can you please find the length of the maximum continuous sequence
of horses on which Bob can make a bet, and remember he will invest money less than
K units. If there is more than one possible combination, Bob will bet randomly on any
one of them.

Given the number of horses(N), reward money(K), and price of betting on N horses in
order.

Hint: For each starting index of a horse, its end index in sequences will be equal
to or greater than the end index of the previous starting index.

Example 1:

Input:
90 100 -> N = 10, K=100
30 40 50 20 20 10 90 10 10 10 -> Price to make bet on each horse in order

Output:
3

Explanation:
There are 10 horses, and the reward money is 100. So, Bob will put money in less than
100. There are two possible o sequences of length three whose total money for betting
is less than 100, i.e. [50 20 20] (sum is 90) and [10 10 10] (sum is 30). Bob will choose
randomly one sequence from these two. As none of the other sequences with a length
greater than 3 will have a price less than 100 so the answer will be 3.

Example 2:
Input:
10 100 -> N = 10, K=100
10 90 80 20 90 60 40 60 70 75 -> Price to make bet on each horse in order

Output:
1

Explanation:
There are no two consecutive horses for which the sum of price is less than 100. So,
Bob will choose randomly any one horse. And the max length of the sequence will be
1.
Constraints:
2<=N<=105
1<= K<=109
1<=A1, A3… AN<=109

The Input format for testing:


The candidate has to write the code to accept 2 inputs.
First Input: It will contain two integers N (number of horses) and K (reward money)
Second Input: It will contain N integers, each separated

VelTech University / 2024 / Page 2 / 27


Question 3)

This vacation you went to visit the golden house. There are N rooms in this golden
house and its owner needs someone to take care of the management of this house. As
you have been unemployed for a long time, you are interested in this job. The owner of
this house wanted an intelligent manager for this role, so he created one puzzle within
that golden house. The person who will be able to solve that puzzle will be the manager
of the golden house.

The owner of the house kept some number of golden coins in each room. You have to
choose two rooms, one from where you will enter and the other one from where you
will exit. From any room either you can exit, or you can move to the next room. While
visiting any room you will collect all the gold coins, and if you enter any room then you
can’t skip collecting gold coins from that room, you have to take those coins. The owner
wants to have exactly K coins, when you exit the room, he guarantees that there will be
at least one possible solution for this puzzle.

Given several rooms (N), and several gold coins in N rooms. You have to find room
numbers from where you will start and from where you will exit. If there is more than
one solution possible, then you have to provide a solution with a smaller starting room
number.

You can assume that room numbers will start from 1 and end at N.
Hint: Find a continuous subsequence whose sum will be exactly equal to K.

Example 1:
Input:
10 15 -> N =10, K = 15
5 3 7 14 18 1 18 4 8 3 -> Number of gold coins in each room.
Output:
13
Explanation: There are ten rooms in the house. You want to have the Total sum of gold
coins in a continuous sequence of rooms to be 15 There are two solutions to this i.e. [1,
3] and [8, 10] then the solution with a smaller starting room number will be printed
hence [1, 3] is output.

Question 4)

Tina was given a piece of silk cloth. There are already a few points or coordinates
mentioned on it. She is creating something which needs an exact square shape
cloth. Help Tina to find out how many minimum points she can add, to cut the perfect
square from the given cloth.
There are already a few points marked on one cloth, Tina has to include the most
number of points from the given points and should try to include the minimum number
of points or coordinates from her side.

VelTech University / 2024 / Page 3 / 27


Find the minimum number of coordinates to achieve the perfect square. Also, try to get
as bigger cloth as possible.
Let us try to understand it with an example
Let’s say there are 3 points given means N=3
These points are:

Let us try to understand it with an example Let’s say there are 3 points given, which
means N=3 These points are
00
22
33

We can have 2 additional coordinates:


(2,0) & (0,2)
OR
(3,0) & (0,3)
Tina doesn’t want to lose a good deal here, so better if she would go with (3,0) &
(0,3). Hence 2 additional coordinates were required to cut a perfect square from that
piece of cloth.
Hence the answer is 2
Example 1:
Input:
5 -> Input integer, N
0 0 -> Input integer, x[i], y[i]
100 100 -> Input integer, x[i], y[i]
200 200 -> Input integer, x[i], y[i]
100 0 -> Input integer, x[i], y[i]
0 100 ->Input integer, x[i], y[i]
Output:
0 -> Output
Explanation:
In the above scenario, we can already make a square from the given coordinates
P1: (0, 0)
P2: (100, 0)
P3: (0, 100)
P4: (100, 100)
Hence no need for any additional coordinates here.
So the answer is No.

Example 2:
Input:
3 -> Input integer. N
00 -> Input integer, x[i], y[i]
22 -> Input integer, x[i], y[i]
33 -> Input integer, x[i], y[i]
Output:
2 -> Output

VelTech University / 2024 / Page 4 / 27


Explanation:
In the above scenario, we can have 2 additional co-ordinates:
(20)&(0,2)
OR
(3,0)&(0.3)
Tina doesn’t want to lose a good deal here so better if she would go with (3,0), (0,3).
Hence 2 additional co-ordinates were required to cut a perfect square from that piece of
cloth.
Hence the answer is 2.

Question 5)

Jack is a sports teacher at St. Patrick’s School. He makes games not only to make the
student fit but So smart. So, he lined up all the N number classes. of students in his
class. At each position, he has fixed a board with the Integer number printed on it.

Each number is unique and in exactly the range of N. Let us say there are 10 students,
then the boards will be printed with numbers from 1 to 10 in a random order given by
the sequence A[ ]. As a rule, all students wear a jersey with their numbers printed on it.
So if there are students, each will have a unique jersey number just like a football team.

Now, in the beginning, all the students will stand as per the increasing order of their
jersey numbers, from left to right. The only difference will be their respective board
number which is placed at their respective location. The board location is fixed and
cannot be changed. We can consider the arrangement as below. Suppose there are
students, and the board is placed in the order of [2 3 1 5 4].

Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5

Now the game begins.

After every beat of the drum, each student will have to move to that location (index),
where his board is pointing to. In the above case student with jersey #1 is standing with
board #2, so now he will have to move to location #2. Similarly, all the other students
will do.
So after the first beat of the drum, the alignment will be:
Board — 2, 3, 1, 5, 4.

This keeps going on and on until all the students are back to the way they were at the
beginning. So, after 6 beats of the drum, all the students will be aligned the same way
as before.

Given N and the order of the board of the respective positions, find the number of
beats required to bring back the students to their original position.
So, for the above case, the answer is 6

VelTech University / 2024 / Page 5 / 27


Example 1:

Input:
3 -> Input integer, N
{1, 2, 3} ->Input integer. B[], board alignment.
Output:
1 -> Output
Explanation:
All the students will be standing in board positions;
Board — 1, 2, 3
Student’s Jersey –1, 2, 3
After the first beat of the drum:
Jersey #1 will move to index 1.
Jersey #2 will move to index 2.
Jersey #3 will move to index 3.
Hence, they will be back in their position in just 1 beat.
So, the answer is 1.

Example 2:

Input:
5 -> Input integer, N
{2, 3, 1, 5, 4} -> Input integer, B[ ], board alignment.
Output:
6 -> Output
Explanation:
All the students will be standing as below, with the board positions:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
After Beat-1of the drum:
Jersey #1 has moved to index 2.
Jersey #2 has moved to index 3.
Jersey #3 has moved to index 1.
Jersey #4 has moved to index 5.
Jersey #5 has moved to index 4.

Board – 2, 3, 1, 5, 4
Student’s Jersey — 3, 1, 2, 5, 4

After Beat-2 of the drum:


Jersey #3 has moved to index 2.
Jersey #1 has moved to index 3.
Jersey #2 has moved to index 1.
Jersey #5 has moved to index 5.
Jersey #4 has moved to index 4.

Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 4, 5

After Beat-3 of the drum:

VelTech University / 2024 / Page 6 / 27


Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 5, 4

After Beat-4 of the drum:


Board — 2, 3, 1, 5, 4
Student’s Jersey — 3, 1, 2, 4, 5

After Beat-5 of the drum:


Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 5, 4

After Beat-6 of the drum:


Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5

Hence, they will be back in their positions after 6 beats.


So, the answer is 6.

Q 6)
A washing machine works on the principle of Fuzzy System, the weight of clothes put
inside it for washing is uncertain but based on weight measured by sensors and the
water level chosen, it decides total time needed.
 For low level water, the time estimate is 25 minutes, where approximately
weight is between 2000 grams or any nonzero positive number below that.
 For medium level water, the time estimate is 35 minutes, where approximately
weight is between 2001 grams and 4000 grams.
 For high level water, the time estimate is 45 minutes, where approximately
weight is above 4000 grams.
Assume the capacity of machine is maximum 7000 grams. When the weight is zero,
time needed is 0 minutes. If the weight exceeds maximum weight limit, then, print
“OVERLOADED”, and for all negative weights, the output is “INVALID INPUT”.
Sample Input-1: 2000, L
Sample Output-1: Time Estimated: 25 minutes
Input should be in the form of integer value.
Output must have the following format: Time Estimated: NN Minutes

Question 7)

At the security checkpoint, airport security personnel have seized a number of travellers’
belongings. Everything has been thrown into a big box (array). Each product carries a
specific level of risk [0,1,2]. The risk severity of the items in this case is represented by
an array [] of N integer values. Sorting the elements in the array according to the degrees
of danger is the task at hand. Between 0 and 2 are the risk values.

VelTech University / 2024 / Page 7 / 27


Example:
Input:
7 -> Value of N
[1,0,2,0,1,0,2]-> Element of arr [0] to arr[N-1], while input each element is separated
by new line.
Output:
0 0 0 1 1 2 2 -> Element after sorting based on risk severity
Example 2:
input : 10 -> Value of N
[2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input each element is
separated by a new line.
Output :
0 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity.
Explanation:
In the above example, the input is an array of size N consisting of only 0’s, 1’s and 2s.
The output is a sorted array from 0 to 2 based on risk severity.

Question 8

For all of its products, a supermarket maintains a pricing structure. Each product has a
value N printed on it. The price of the item is determined by multiplying the value N,
which is read by the scanner, by the sum of all its digits. The goal here is to create
software that, given the code of any item N, will compute the product (multiplication)
of all the value digits (price).

Example 1:

Input :
5244 -> Value of N

Output :
160 -> Price

Explanation:
From the input above
Product of the digits 5,2,4,4
5*2*4*4= 160
Hence, output is 160.

VelTech University / 2024 / Page 8 / 27


Question 9

There are two banks – Bank A and Bank B. Their interest rates vary. You have received
offers from both banks in terms of the annual rate of interest, tenure, and variations of
the rate of interest over the entire tenure. You have to choose the offer which costs you
the least interest and reject the other. Do the computation and make a wise choice.

The loan repayment happens at a monthly frequency and Equated Monthly Installment
(EMI) is calculated using the formula given below :
EMI = loan amount * monthly interest rate / ( 1 – 1 / (1 + monthly interest rate)^(number
of years * 12))
Constraints:
1 <= P <= 1000000
1 <=T <= 50
1<= N1 <= 30
1<= N2 <= 30
Input Format:
First line: P principal (Loan Amount)
Second line: T Total Tenure (in years).
Third Line: N1 is the number of slabs of interest rates for a given period by Bank A.
The first slab starts from the first year and the second slab starts from the end of the
first slab and so on.
Next N1 line will contain the interest rate and their period.
After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
Next N2 lines are the number of slabs of interest rates for a given period by Bank B.
The first slab starts from the first year and the second slab starts from the end of the
first slab and so on.
The period and rate will be delimited by a single white space.
Output Format: Your decision is either Bank A or Bank B.

Example 1
Input
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Output: Bank B

VelTech University / 2024 / Page 9 / 27


Example 2
Input
500000
26
3
13 9.5
3 6.9
10 5.6
3
14 8.5
6 7.4
6 9.6
Output: Bank A

Question 10
Some prime numbers can be expressed as a sum of other consecutive prime numbers.
For example, 5 = 2 + 3, 17 = 2 + 3 + 5 + 7, 41 = 2 + 3 + 5 + 7 + 11 + 13. Your task is
to find out how many prime numbers which satisfy this property are present in the range
3 to N subject to a constraint that summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the above-mentioned
property in a given range.

Input Format: First line contains a number N


Output Format: Print the total number of all such prime numbers which are less
than or equal to N.
Example:
Input:
20
Output:
2
Explanation:
Below 20, there are 2 such numbers,
5=2+3
17=2+3+5+7

Question 11:
K-th largest factor of N
A positive integer d is said to be a factor of another positive integer N if when N is
divided by d, the remainder obtained is zero. For example, for number 12, there are 6
factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the
number k itself. Given two positive integers N and k, write a program to print the kth
largest factor of N.
Input Format: The input is a comma-separated list of positive integer pairs (N, k)
Output Format: The kth highest factor of N. If N does not have k factors, the output
should be 1.
Constraints: 1<N<10000000000. 1<k<600.You can assume that N will have no prime
factors which are larger than 13.

VelTech University / 2024 / Page 10 / 27


Example 1
Input: 12,3
Output: 4
Explanation: N is 12, k is 3. The factors of 12 are (1,2,3,4,6,12). The highest factor is
12 and the third largest factor is 4. The output must be 4

Example 2
Input: 30,9
Output: 1
Explanation: N is 30, k is 9. The factors of 30 are (1,2,3,5,6,10,15,30). There are only
8 factors. Since given k value is more than the number of factors, the output is 1.

import java.util.*;
class Main { public static void main(String[] args) {
int n, k, i, c = 0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for (i = n; i >= 1; i--) {
if ((n % i) == 0)
c++;
if (c == k) {
System.out.println(i);
break;
}
}
if (c != k)
System.out.println("1");

}
}

Question 12: Collecting Candies

Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat
them later whenever he wants to.
He has recently received N boxes of candies each containing Ci candies where Ci
represents the total number of candies in the ith box. Krishna wants to store them in a
single box. The only constraint is that he can choose any two boxes and store their joint
contents in an empty box only. Assume that there are an infinite number of empty boxes
available.
At a time, he can pick up any two boxes for transferring and if both the boxes contain
X and Y number of candies respectively, then it takes him exactly X+Y seconds of time.
As he is too eager to collect all of them he has approached you to tell him the minimum
time in which all the candies can be collected.

VelTech University / 2024 / Page 11 / 27


Input Format:

The first line of input is the number of test case T


Each test case is comprised of two inputs
The first input of a test case is the number of boxes N
The second input is N integers delimited by whitespace denoting the number of candies
in each box
Output Format: Print minimum time required, in seconds, for each of the test cases.
Print each output on a new line.

Input:
1
4
1234
Output:
19
Explanation:
4 boxes, each containing 1, 2, 3 and 4 candies respectively. Adding 1 + 2 in a new box
takes 3 seconds. Adding 3 + 3 in a new box takes 6 seconds. Adding 4 + 6 in a new box
takes 10 seconds. Hence total time taken is 19 seconds. There could be other
combinations also, but overall time does not go below 19 seconds.

VelTech University / 2024 / Page 12 / 27


Question 13: Square Free Numbers
In the theory of numbers, square free numbers have a special place. A square free
number is one that is not divisible by a perfect square (other than 1). Thus 72 is
divisible by 36 (a perfect square), and is not a square free number, but 70 has factors 1,
2, 5, 7, 10, 14, 35 and 70. As none of these are perfect squares (other than 1), 70 is a
square free number.
For some algorithms, it is important to find out the square free numbers that divide a
number. Note that 1 is not considered a square free number.
In this problem, you are asked to write a program to find the number of square free
numbers that divide a given number.
Input
The only line of the input is a single integer N which is divisible by no prime number
larger than 19
Output
One line containing an integer that gives the number of square free numbers (not
including 1)
Constraints
N < 10^9
Complexity
Simple
Time Limit
1

Example 1
Input
20
Output
3
Explanation
N=20
If we list the numbers that divide 20, they are 1, 2, 4, 5, 10, 20.
1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect
square. 2 and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none
of which are perfect squares. Hence the square free numbers that divide 20 are 2, 5,
10. Hence the result is 3.

Example 2
Input
72
Output
3
Explanation
N=72. The numbers that divide 72 are
1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72
1 is not considered square free. 4, 9 and 36 are perfect squares, and 8,12,18,24 and 72
are divisible by one of the. Hence only 2, 3 and 6 are square free. (It is easily seen that
none of them are divisible by a perfect square). The result is 3

VelTech University / 2024 / Page 13 / 27


Question 14: Code and Sum

Given N number of numbers, perform the following operations and print the output.
You have to efficiently handle potentially large numbers (x) by focusing only on the
last two digits of 2^x.
Your program needs to read N integers. For each integer x, it has to calculate 2^x,
extract the last two digits of this number, and add these digits to a running total sum.
Finally, it has to print the last two digits of the total sum.

Input
First line contains an integer N
Second line will contain N numbers delimited by space

Output
Number that is the output of the given code by taking inputs as specified above

Example 1
Input
4
8674
Output
64

Example 2
Input
3
123
Output
14

Explanation

1. Imports and Setup:

import java.util.Scanner;

This imports the Scanner class, which is used for reading input from the user.

2. Initialization:

Scanner sc = new Scanner(System.in);


long sum = 0;

o Scanner sc = new Scanner(System.in); creates a Scanner object


to read input from the standard input (typically the keyboard).
o long sum = 0; initializes a variable sum to accumulate the result.
3. Read Number of Inputs:

int N = sc.nextInt();

VelTech University / 2024 / Page 14 / 27


This line reads an integer N, which represents the number of subsequent
integers the program will process.

4. Loop through N Inputs:

for (int i = 0; i < N; i++) {


final long x = sc.nextLong();

This for loop iterates N times to process each integer x.

5. Calculate 2^x and Process:

java
Copy code
String str = Long.toString((long) Math.pow(1 << 1, x));

o 1 << 1 performs a bitwise left shift, which effectively computes 2.


Hence, 1 << 1 is 2.
o Math.pow(2, x) computes 2^x (using the bitwise left shift result as
the base).
o Long.toString(...) converts the result of 2^x to a String.

str = str.length() > 2 ? str.substring(str.length() - 2) : str;

oThis line ensures that only the last two digits of the computed power
are kept.
o If the length of the string representation of 2x2^x2x is greater than 2, it
keeps only the last two characters. If it’s less than or equal to 2, it uses
the whole string.
6. Add to Sum:

sum += Integer.parseInt(str);

oConverts the last two digits (stored as a string) to an integer.


oAdds this integer to sum.
7. Output the Result:

System.out.println(sum % 100);

o Prints the last two digits of sum by taking sum % 100.

Summary:

 The program reads N integers. For each integer x, it calculates 2^x, extracts the
last two digits of this number, and adds these digits to a running total sum.
Finally, it prints the last two digits of the total sum.

This code efficiently handles potentially large numbers by focusing only on the last
two digits of 2^x, making it suitable for scenarios where you only need the last two
digits of results from operations involving powers of 2.

VelTech University / 2024 / Page 15 / 27


Question 15: Houses Problem

There are n houses built in a line, each of which contains some value in it. A thief is
going to steal the maximal value of these houses, but he can’t steal in two adjacent
houses because the owner of the stolen houses will tell his two neighbors left and right
side. What is the maximum stolen value?

Input Format
First an integer n, denoting how many houses are there.
Then n space separated integers denoting the values for the n houses.
Output Format
An integer denoting the maximum value possible to steal.

Input
7
6713825
Output
20

Explanation
6+1+8+5 = 20.
It is the max possible value.

Question 16

Juan Marquinhos is a geologist and he needs to count rock samples to send them to a
chemical laboratory. He has a problem: The laboratory only accepts rock samples in a
range of its size in ppm (parts per million).
Juan Marquinhos receives the rock samples one by one and he classifies the rock
samples according to the range of the laboratory. This process is very hard because the
number of rock samples may be in the millions.
Juan Marquinhos needs your help, your task is to develop a program to get the number
of rocks in each of the ranges accepted by the laboratory.

Input Format
A positive integer S (the number of rock samples) separated by a blank space, and a
positive integer R (the number of ranges of the laboratory); A list of the sizes of S
samples (in ppm), as positive integers separated by space R lines where the ith line
containing two positive integers, space separated, indicating the minimum size and
maximum size respectively of the ith range.

Output Format
R lines where the ith line contains a single non-negative integer indicating the number
of the samples which lie in the ith range.
Constraints
10 < S < 10000
1 < R < 1000000
1 size of each sample (in ppm) < 1000

VelTech University / 2024 / Page 16 / 27


Example 1
Input: 10 2
345 604 321 433 704 470 808 718 517 811
300 350
400 700
Output: 2 4
Explanation:
There are 10 samples (S) and 2 ranges ( R ). The samples are 345 604 321 433 704 470
808 718 517 811. The ranges are 300-350 and 400-700. There are 2 samples in the first
range (345 and 321) and 4 samples in the second range (604, 433, 470, 517). Hence the
two lines of the output are 2 and 4

Example 2
Input: 20 3
921 107 270 631 926 543 589 520 595 93 873 424 759 537 458 614 725 842 575 195
1 100
50 600
1 1000

Output: 1 12 20

Explanation:
Consider the Input: 20 3 which means 20 numbers followed by 3 more lines of
input.
20 numbers are  921 107 270 631 926 543 589 520 595 93 873
424 759 537 458 614 725 842 575 195

range check 1  1 100


range check 2  50 600
range check 3  1 1000

There are 20 samples and 3 ranges. The ranges are 1-100, 50-600, and 1-1000.
Note that the ranges are overlapping.
The number of samples in each of the three ranges is 1, 12, and 20 respectively.

Note that, the number of samples in range 1 to 100 : 1. The number of samples in
range 50to 600: 12. The number of samples in range 1 to 1000 : 20 (which means
that all the 20 numbers are in the range 1 to 1000).

Hence the three lines of the output are 1, 12, and 20.

VelTech University / 2024 / Page 17 / 27


Q17) You’re given with the size of the array and an array of integers; print the
number of times each integer has occurred in the array.

Sample Input 1:

10
1233414512
Sample Output 1:

1 occurs 3 times
2 occurs 2 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
Q18) Problem Statement –

Write a method to solve the following equation a3 + a2b + 2a2b + 2ab2 + ab2 + b3.
Write a program to accept three values in order of a, b and c and get the result of the
above equation.

Q19) Problem Statement – Your job is to calculate how many tyres would be there
in each dealership and find the total number of tyres.

Input

42
40
12

Output
44
Explanation:

There are total 3 dealerships: Dealerships1 contains 4 cars and 2 bikes. Dealerships2
contains 4 cars and 0 bikes. Dealerships3 contains 1 car and 2 bikes

Total number of tyres in dealerships1 is (4 x 4) + (2 x 2) = 20


Total number of tyres in dealerships2 is (4 x 4) + (0 x 2) = 16
Total number of tyres in dealerships3 is (1 x 4) + (2 x 2) = 8.
Total tyres = 20+16+8 = 44.

VelTech University / 2024 / Page 18 / 27


Q20) Write a program to convert 4 digit numbers to equivalent English words.

Sample input-1: 1234

Sample output-1: one thousand two hundred thirty-four

Sample input-2: 9999

Sample output-2: nine thousand nine hundred ninety-nine

Q21) Write a Program to Find the Roots of a Quadratic Equation. If roots are
possible, then, there are four cases to be handled as explained below:

Case1) Roots are real and different. For this case, print “RD”.
Case2) Roots are real and same. For this case, print “RS”.
Case3) Roots are complex. For this case, print “RC”.
Case4) In case of wrong inputs, print “Invalid”.

Q22) Ritik wants a magic board, which displays a character for a corresponding
number for his science project. Help him to develop such an application.

For example, when the digits 65,66,67,68 are entered, the alphabet ABCD are to be
displayed.

[Assume the number of inputs should be always 4 ]

Sample Input 1:

Enter the digits:


65
66
67
68
Sample Output 1:

65-A
66-B
67-C
68-D

VelTech University / 2024 / Page 19 / 27


Q 23) Chaman planned to choose a four-digit lucky number for his car. His lucky
numbers are 3,5 and 7. Help him find the number, whose sum is divisible by 3 or
5 or 7. Provide a valid car number. If the input fails to satisfy the condition, then
display as unlucky number.

Note: The input other than 4-digit positive number [includes negative and 0] is
considered as invalid.

Sample Input 1:1234


Sample Output 1: LUCKY

Sample Input 2: 1214


Sample Output 2: UNLUCKY

Q24) Rat Count House


The method accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’
of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is
the amount of food each rat consumes and each ith element of array ‘arr’ represents the
amount of food present in ‘i+1’ house number, where 0 <= i
Note:

 Return -1 if the array is null


 Return 0 if the total amount of food from all houses is not sufficient for all the
rats.
 Computed values lie within the integer range.

Sample Input:

 7 – number of rats
 2 – food needed for each rat
 8 – number of houses
 2 8 3 5 7 4 1 2 – food available in each house

Sample Output: 4 HOUSES TO BE VISITED TO SATISFY ALL RATS

Explanation: Total amount of food required for all rats = r * unit; which is = 7 * 2 =
14. The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4
houses is sufficient for all the rats. Thus, output is 4.

Q25) Problem Description:

The Binary number system only uses two digits, 0 and 1 and number system can be
called binary string. You are required to implement the following method:

int OperationsBinaryString(String str);

The method accepts a string str as its argument. The string str consists of binary digits
separated with an alphabet as follows: A denotes AND operation; B denotes OR

VelTech University / 2024 / Page 20 / 27


operation; C denotes XOR operation. You are required to calculate the result of the
string str, scanning the string to right taking one operation at a time, and return the
same.

Note: 1) No order of priorities of operations is required. 2) Length of str is odd.

3) If str is NULL or None (in case of Python), return -1

Sample Input: str: 1C0C1C1A0B1

Sample Output:1

Explanation: The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1
AND 0 OR 1”, result of the expression becomes 1, hence 1 is returned.

Q26)

N-base notation is a system for writing numbers that uses only n different symbols,
This symbols are the first n symbols from the given notation list(Including the symbol
for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9,
10:A,11:B and so on upto 35:Z)
Implement the following method Char[] DectoNBase(int n, int num): The method
accept positive integer n and num Implement the method to calculate the n-base
equivalent of num and return the same as a string. Steps:
Divide the decimal number by n, Treat the division as the integer division
Write the the remainder (in n-base notation)
Divide the quotient again by n, Treat the division as integer division
Repeat step 2 and 3 until the quotient is 0
The n-base value is the sequence of the remainders from last to first
Assumption: 1 < base < = 36
Example Input:
12 base
718  n
Output
4BA
Explanation

Iteration Given Given Quotient Remainder


Number number Divisor
1 718 12 59 10 which is coded as A
2 59 12 4 11 which is coded as B
3 4 12 0 4 which is coded as 4

VelTech University / 2024 / Page 21 / 27


Q27)
Problem Statement
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two
numbers from right-to-left one digit at a time
You are required to implement the following method.
Int NumberOfCarries(int num1 , int num2);
The methods accepts two numbers ‘num1’ and ‘num2’ as its arguments. You are
required to calculate and return the total number of carries generated while adding
digits of two numbers ‘num1’ and ‘ num2’.
Assumption: num1, num2>=0
C 11

Input 1: 451

349

800

Output 1: 2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is
carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned.
Sample Input2:
Num 1: 23
Num 2: 563
Sample Output2: 0

VelTech University / 2024 / Page 22 / 27


Q28) Sweet Seventeen
Given a maximum of four digits to the base 17(10 -> A, 11 -> B, 12 -> C, 16 -> G) as
input, output its decimal value.
Sample Input: 23GF
Sample Output: 10980

Q29) Word is the key


One programming language has the following keywords that cannot be used as
identifiers:
Write a program to find if the given word is a keyword or not.
One programming language has the following keywords that cannot be used as
identifiers:
List of keywords = {break, case, continue, default, defer, else, for, func, goto, if, map,
range, return, struct, type, var}
Sample Input #1: defer
Output: defer is a keyword

Sample Input #2: While


Sample Output: while is not a keyword

Q30)
A party has been organised on cruise. The party is organised for a limited time(T). The
number of guests entering (E[i]) and leaving (L[i]) the party at every hour is represented
as elements of the array. The task is to find the maximum number of guests present on
the cruise at any given instance within T hours.
Sample Input 1: 5
7,0,5,1,3
1,2,1,3,4
Sample Output :8
Explanation:
1st hour: Entry : 7 Exit: 1
No. of guests on ship : 6
2nd hour : Entry : 0 Exit : 2
No. of guests on ship : 6-2=4
Hour 3: Entry: 5 Exit: 1
No. of guests on ship : 4+5-1=8
Hour 4: Entry : 1 Exit : 3
No. of guests on ship : 8+1-3=6
Hour 5: Entry : 3 Exit: 4
No. of guests on ship: 6+3-4=5
Hence, the maximum number of guests within 5 hours is 8.
The input format for testing
The candidate has to write the code to accept 3 input.
First input- Accept value for number of T (Positive integer number)
Second input- Accept T number of values, where each value is separated by a
new line.
Third input- Accept T number of values, where each value is separated by a
new line.

VelTech University / 2024 / Page 23 / 27


The output format for testing
The output should be a positive integer number or a message as given in the problem
statement (Check the output in Example 1)
Constraints:
1<=T<=25
0<= E[i] <=500
0<= L[i] <=500

VelTech University / 2024 / Page 24 / 27


Q31) You’re supposed to reduce the size of this string using mathematical logic given
as in the example below:

Sample Input-1: aabbbbeeeeffggg

Sample Output -1: a2b4e4f2g3

Sample Input -2: abbccccc

Sample Output- 2: ab2c5

Q32) You have write a method that accepts, a string which length is “len”, the string
has some “#”, in it you have to move all the hashes to the front of the string and return
the whole string back and print it.

Method Syntax: String moveHash (String str);

Sample input 1: Move#Hash#to#Front

Sample Output: ###MoveHashtoFront

Question 33)

Ayush is working on a strange algorithm where he wants to convert a string from A to


B, both the strings of equal length N
Below are the rules which can be performed to convert a string

String A and B are of equal length

Both of them are in lower case

Choose a subset X from the string A, between the index 1 and N.

Let ‘s’ be the letter that alphabetically comes before all other letters in the subset. Let
‘s’ be called the ‘smallest element’ in the subset.

Replace all the elements of the subset with the letter ‘s’

Find the minimum number of moves which is required to perform the conversion. If it
is not possible to convert the string from A to B then return -1

Let us try to understand it with examples Suppose there are 2 strings


A = abcab

VelTech University / 2024 / Page 25 / 27


B = aabab
Operation 1:
Now we have chosen a subset S, let us say we have taken indexes 2,3,5 from A then
the subset S becomes [bcb]. Next, we have to choose the smallest element, 6041 here,
which is b here in b & c. Next, we have to replace all the other elements in the subset
with this element. So ‘b’ with replace everything in [bcb]. which becomes [bbb]. Now
we will place all the respective elements back in their respective index. This will
update the original string as [abbab].

Operation 2:
Original string [abbab]. Now we have chosen a subset S, let’s say we have taken an
index 1,2,4 from A then the subset becomes [aba]. Next, we have to choose the
smallest element, which is here in a & b. Next, we have to replace the smallest with
all the other elements in the subset. So ‘a’ will replace everything in [aba]. Now we
will place all the respective elements back in their respective index. This will update
the original string as [aabab]. This is the same as String B. Hence it is possible to
convert string A to B, with 2 operations.
So, the answer is 2.

Example 1:
Input:
2 -> Input integer, N
de -> input string, A
cd -> Input string, B

Output: -1 -> Output

Explanation:
In the above example, we can see that there is an alphabet in A which is completely
different from B. hence it is not possible to convert A to B
So the answer is -1

Example 2:
Input:
4-> input integer, N
abab-> input string, A
abaa-> input string A

Output:
1 -> Output

VelTech University / 2024 / Page 26 / 27


Explanation:
Operation 1:
Now we have chosen a subset S, let’s say we have taken
index 3, 4 from A
Then Subset S becomes [ab]
Next, we have to choose the smallest element, which is a here in a & b
Next, we have to replace the smallest with all the other elements in a subset. So ‘a’
will replace everything in [abl, which becomes [aa]
Now we will place all the respective elements back in their respective index. This will
update the original string as [abaa]
This is the same as String B
Hence it is possible to convert string A to B. with 1 operation. So, the answer is 1.

VelTech University / 2024 / Page 27 / 27

You might also like