Infytq-Practice Sheet Problems
Infytq-Practice Sheet Problems
Infytq-Practice Sheet Problems
1. Concatenation
Input: a string of comma separated numbers. The numbers 5 and 8 are present in the list. Assume that
8 always comes after 5.
Case 1: num1 = add all numbers which do not lie between 5 and 8 in the input.
Case 2: num2= numbers formed by concatenating all numbers from 5 to 8.
Output: sum of num1 and num2
Example: 1) 3,2,6,5,1,4,8,9
Num1 : 3+2+6+9 =20
Num2: 5148
output: 5148+20 = 5168
2. Palindrome
Write a python function nearest_palindrome() which can accepts a number and return the nearest
greater palindrome number.
Input: 12300 -> output: 12321
Input: 12331 -> output: 12421
3. Parenthesis matching
A non-empty string instr containing only parenthesis (,),{.},[,] it return outstr based on following,
– instr is properly nested and return 0
– instr not properly nested, return position of element in instr
– position start from 1
Input: {([])} Output: 0
Input: (])()] Output: 3
Input: [[()] Output: n+1 for last element i.e 5+1=6
4. Sum of factor
For a given list of numbers find the its factors and add the factors then if the sum of all factor is
present in original list, sort it and print it
Ex:
Input: 0,1,6
Factors: 0 = 0, sum =0
1 = 1, sum =1
6 =1,2,3 = sum =6
Output: 0,1,6
If the sum is not present in the list, then return -1.
6. Password generation
Given input of array of string in format <emnp name> <emp number> separated by ‘,’ Emp
should contain only alphabets and employee number. You have to generate password for
Ex:
input: Robert: 36787, Tina: 68721, Jo:56389
Output: tiX
Conditions: len of robert is 6 and 6 is present in emp number
robert (36787), so return the alphabet at position 6 that is t.
Now len of tina is 4 and 4 is not present in the 68721 so select the number which is max and less than
the len of tina so select 2 return the alphabet that is at position 2 that is i.
Now In of Jo is 2 it is not present in 56389 and there is not present any number which is less than 2 so
return X.
7. Even number
A string which is a mixture of letter and integer and special char from which find the largest even
number that can generated from the available digit after removing the duplicates.
If an even number is not formed then return-1.
Ex: infosys@337
O/p:-1
Hello#81@21349
O/p:983412
8. Matrix problem
Read m’m>4
N=m+1
Take m*n matrix
If any num is consecutive for 3 times either in a row, column, diagonals print the num, if there
multiple num print min of those num
Ex: m=6 take 6*7 matrix
23456243
23476762
23555525
23112136
11119035
23115127
O/p=1
9. String rotation
Input: rhdt:246, ghftd:1246
Expl: here every string is associated with the number separated by : if sum of squares of digits is even
then left rotate the string by 1, if square of digits is odd then rotate the string right by 2 position.
Problem Statement – Abhijeet is one of those students who tries to get his own money by part time jobs in
various places to fill up the expenses for buying books. He is not placed in one place, so what he does, he tries
to allocate how much the book he needs will cost, and then work to earn that much money only. He works and
then buys the book respectively. Sometimes he gets more money than he needs so the money is saved for the
next book. Sometimes he doesn’t. In that time, if he has stored money from previous books, he can afford it,
otherwise he needs money from his parents.
Now his parents go to work and he can’t contact them amid a day. You are his friend, and you have to find how
much money minimum he can borrow from his parents so that he can buy all the books.
He can Buy the book in any order.
Function Description:
Complete the function with the following parameters:
Name Type Description
N Integer How many Books he has to buy that day.
EarnArray[ ] Integer array Array of his earnings for the ith book
CostArray[ ] Integer array Array of the actual cost of the ith book.
Constraints:
Problem Statement – Aashay loves to go to WONDERLA, an amusement park. They are offering students
who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.
They will give some k turns to remove the cost of one ticket where the cost of tickets are combined and given as
string. Help Aashay in coding as he is not good in programming and get a 50% discount for your ticket.
Constraints:
The first line contains a string, Tickets, denoting the given cost of each ticket.
The next line contains an integer, K, denoting the number of tickets that is to be removed.
Sample Cases:
Sample Input 1
203
3
Sample Output 1
0
14. HR issues
Problem statement -: Shovon is an HR in a renowned company and he is assigning people to work. Now he is
assigning people work in a fashion where if he assigns somework a work of cost 2, the next person will be
strictly getting a job with cost equal or more than 2. Given that Shovon’s company has infinite work and a
number of employees, how many distributions can be possible. The cost of jobs can go 0 to 9.
Function Description:
Complete the special_numbers function in the editor below. It has the following parameter(s):
Parameters:
Name Type Description
N Integer The number of depts.
arr[ ] Integer array The number of employees in each dept..
Return: The function must return an INTEGER denoting the sum of answers for all distinct distributions.
Constraints:
1 <= n <= 100
1 <= arr[i] <= 200
Sample Cases:
Sample Input 1
2
4
1
Sample Output 1
725
Description
The ans if m = 1 is 1o, which is all numbers from 0 to 9
The ans for m = 2 is 55
The answer for m = 3 is 220
The answer for m = 4 is 715
So fun(4) + fun(1) = 725
Function Description:
Complete the weight machine function in the editor below. It has the following parameter(s):
Parameters:
Name Type Description
N Integer number of luggage
T Integer weight of each luggage
weights[ ] Integer array threshold weight
Returns: The function must return an INTEGER denoting the required amount to be paid.
Constraints:
Sample Cases:
Sample Input 1
4
1
2
3
4
3
Sample Output 1
5
Print out the maximum even number which can be generated by making combinations of
numbers present in the given string. Each number can be used only once i.e. No Redundancy.
Example 1
Input String = Infytq@218412
Intermediate step (List of numbers *//redundancy removed*) = [2,1,8,4]
Output Number = 8412
Example 2
Input : Hello#81@21349
Output: 983412
Example 3
Input String = someString&337
Intermediate step (List of numbers *//redundancy removed*) = [3,7]
Output Number = -1
17. Uniformity
You are given a string that is formed from only three characters ‘a’, ‘b’, ‘c’. You are allowed to
change atmost ‘k’ characters in the given string while attempting to optimize the uniformity index.
Note: The uniformity index of a string is defined by the maximum length of the substring that
contains same character in it.
Input
The first line of input contains two integers n (the size of string) and k. The next line contains a string
of length n.
Output
A single integer denoting the maximum uniformity index that can be achieved.
Constraints
1 <= n <= 10^6
0 <= k <= n
abaccc
Sample Output 0
6
Explanation
First 3 letters can be changed to ‘c’ and we can get the string ‘cccccc’
For a given positive number num, identify the palindrome formed by performing the following
operations-
For example:
If original integer is 195, we get 9,339, as the resulting palindrome after the fourth addition:
Example 1
Sample input:
124
Sample output:
545
Explanation:
The sum of 124 and its reverse 421 is 545 which is a palindrome.
Example 2
Sample input:
4
Sample output:
8
Explanation:
The sum of 4 and its reverse 4 is 8 which is a palindrome.
For a given string with letters and special characters, print the string with the letters reversed,
but without changing the position of the special characters.
Output: qt@#y^fni&!
Multiply with each of the numbers, num2 of M2 such that num2 is at M2[r1][r1],
M2[r1][c1], M2[c1][r1], M2[c1][c1], if exists.
Identify and add the product that occurs maximum number of times to outmatrix[r1]
[c1].
If there are multiple products with maximum number of times, then the product with
the lowest value will be held by outmatrix[r1][c1].
If both r1 and c1 don’t exist in M2, then outmatrix[r1][c1] = -1
Input Format:
Example:
Input:
2
2
12
34
3
4
Output: 3 6
9 -1
Explanation:
• For (0, 0): Here r1=0 and c1=0. Multiplying with the element at (0,0) of M2
results in 1*3=3. The only product is 3 which is occurring once. Hence
outmatrix[0][0] = 3
• For (0, 1): Here r1=0 and c1=1. Multiplying with each of the elements at (0,0),
(0,1), (1,0), (1,1) of M2 results in 2*3=6, 2*4=8. (0,1) & (1,1) don't exist in M2
and hence ignored. Products are '6' occurring once and '8' occurring once.
Both the products are occurring once. Hence substituting the lowest value
product i.e. '6' to outmatrix at (0,1).
• For(1, 0): Here r1=1 and c1=0. Multiplying with each of the elements at (0,0)
(0,1), (1,0), (1,1) of M2 results in 3*3=9, 3*4=12, (0,1) & (1,1) don't exist in
M2 and hence ignored. Products are '9' occurring once and '12' occurring
once. Both the products are occurring once. Hence substituting the lowest
value product i. e. '9' to outmatrix at (1,0)
• For (1, 1): Here r1=1 and c1=1, (1,1) doesn't exist in M2. Hence
outmatrix[1][1] = -1
Given a string and a number separated by a colon(:), output the string as follows:
• If the sum of squares of the digits of the number following the string is even, rotate the
string to right by 1 and print it.
• If the sum of squares of the digits of the number following the string is odd, rotate the
string to the left by 2 and print it.
Example 1:
Input: dkns:348
Output: nsdk
Explanation: Sum of squares of digits of 348 are: 3*3 + 4*4 + 8*8 = 9+16+64 = 89, which is
odd, therefore left rotation by 2 places.
Example 2:
Input: jaipq:974
Output: qjaip
Explanation: 9*9 + 7*7 + 4*4 = 81 + 49 + 16 = 146, which is even, hence right rotation by 1.
22. Lane Problem
Problem: A, B, C, D are lanes with capacity of 10 slots each.
Input Format:
i) Point 1: First take the inputs for the 4 lanes with booked slots 1-10 for each Lane (4 inputs
in 4 lines and the values are separated by commas)
ii) Point 2: If no slot is booked for a lane ‘-1’ will be given as an input for that lane.
iii) Point 3: At 5th line take input for number of waiting cars.
To perform:
Point 1: For all waiting cars to be parked first find out the lane with maximum free slots if 2
lane have the same number of highest free slots, then prefer A -> D flow.
Point 2: Fill each waiting car, in the selected lane and mark it as the sequence number for
the lane with free slots A, A2,…….A10 till all waiting cars are parked. If A has already filled 5
slots then fill waiting slots from A6-A10
Point 3: If the waiting cars are not completely filled, then fill the next highest free lane
continue till all waiting cars get parked.
Note :If no slot is free and cars can’t be parked then print capital ‘X‘ (without quotes).
Sample Input:
A1,A2,A3,A4
B1,B2,B3
C1,C2
D1,D2,D3,D4,D5
10
Sample Output:
C3 C4 C5 C6 C7 C8 C9 C10 B4 B5
Explanation: Here the Slot C has the highest number of available spaces (10 (total space) – 2
(Occupied spaces) = 8 (left out spaces) ). So first 8 cars out of 10 cars in the waiting list are
parked in the slot, the remaining 2 cars are parked into Slot B, which is the second highest
slot with available spaces (10-3 = 7).
23. Unique Pair whose Sum of Digits are Same
Problem: Consider a non-empty input array (inarr) containing non-zero positive Integer.
From input array, identify the unique pairs of integers such that each of the integers in the
pair have the same sum of the digits. Print outnum, the number of unique pairs identified
satisfying the criteria. If no such pair of integers can be identified print -1.
Input format: Read the array inarr (input array) with the elements separated by ‘,’ (comma)
Input format: First line contains the given set of numbers. Second line contains a single
integer denoting the sum.
Sample Input:
-1,1,0,2, -2
0
Sample Output:
3
Explanation: All the possible combinations of length 4 which will add up to be 0 are (-1, 1, 2,
-2), (0, 0, 1, -1), (0, 0, -2, 2)
Input Format: The first line contains an integer T. T is the number of independent testcases.
For each testcase the first line contains an integer denoting the number of elements you can
delete. Next line contains the list. (Inputs are separated by space here.)
Sample Input:
2
2
1124
3
111222456
Sample Output:
1
2
Explanation: Here, 2 and 4 are removed so that it has the minimum distinct element 1 which
is repeated (repetition are removed) so the length of the minimum distinct list is 1
For the second case, 4, 5 and 6 are removed so that it has the minimum distinct element 1 and
2 which is repeated (repetition are removed) so the length of the minimum distinct list is 2
Given two strings A and B of length N, the task is to check whether the two strings can be
made equal by swapping any character of A with any other character of B only once.
Examples:
Input: A = “SEEKSFORGEEKS”, B = “GEEKSFORGEEKG”
Output: Yes
“SEEKSFORGEEKS” and “GEEKSFORGEEKG”
can be swapped to make both the strings equal.
Ref: https://www.geeksforgeeks.org/check-if-two-strings-can-be-made-equal-by-swapping-
one-character-among-each-other/
27. Longest subsequence having difference atmost K
Given a string S of length N and an integer K, the task is to find the length of longest sub-
sequence such that the difference between the ASCII values of adjacent characters in the
subsequence is not more than K.
Examples:
Input: N = 7, K = 2, S = "afcbedg"
Output: 4
Explantion:
Longest special sequence present
in "afcbedg" is a, c, b, d.
It is special because |a - c| <= 2,
|c - b| <= 2 and | b-d| <= 2
Ref: https://www.geeksforgeeks.org/longest-subsequence-having-difference-atmost-k/
Examples:
Input: str = “geeksforgeeks”
Output: No
Input: str = “madam”
Output: Yes
Ref: https://www.geeksforgeeks.org/check-whether-the-given-string-is-palindrome-using-
stack/
Ref: https://www.geeksforgeeks.org/minimum-number-of-operations-to-convert-a-given-
sequence-into-a-geometric-progression/
31. Minimum operations required to convert X to Y by multiplying X with the given co-
primes
Given four integers X, Y, P and Q such that X ≤ Y and gcd(P, Q) = 1. The task is to find
minimum operation required to convert X to Y. In a single operation, you can
multiply X with either P or Q. If it is not possible to convert X to Y then print -1.
Examples:
Input: X = 7, Y = 9, P = 2, Q = 7
Output: -1
There is no way we can reach 9 from 7 by
multiplying 7 with either 2 or 7
Ref: https://www.geeksforgeeks.org/minimum-operations-required-to-convert-x-to-y-by-
multiplying-x-with-the-given-co-primes/
32. Minimum number of operations required to reduce N to 1
Given an integer element ‘N’, the task is to find the minimum number of operations that need
to be performed to make ‘N’ equal to 1.
The allowed operations to be performed are:
1. Decrement N by 1.
2. Increment N by 1.
3. If N is a multiple of 3, you can divide N by 3.
Examples:
Input: N = 4
Output: 2
4–1=3
3/3=1
The minimum number of operations required is 2.
Input: N = 8
Output: 3
8+1=9
9/3=3
3/3=1
The minimum number of operations required is 3.
Ref: https://www.geeksforgeeks.org/minimum-number-of-operations-required-to-reduce-
n-to-1/
33. Island
You are a poor person in an island. There is only one shop in this island, this shop is
open on all days of the week except for Sunday. Consider following constraints:
Explanation 2: You can’t survive even if you buy food because the maximum number
of units you can buy in one day is less the required food for one day.
Sample Test Cases
Input Output
Test Case 1 10 14 3 3
Test Case 2 12 9 8 NO
Test Case 3 15 7 5 11
Test Case 4 18 17 12 13
Test Case 5 10 6 2 4
Test Case 6 10 8 5 7
Test Case 7 12 9 8 NO
Test Case 8 12 10 2 3
34. Power of 2
Write a program to find whether a given number is a power of 2 or not.
Input format:
The first line of the input contains the number n for which you have to find whether it
is a power of 2 or not.
Output Format:
Print 'YES' or 'NO' accordingly without quotes.
Example:
Input:
32
Output:
YES
Input:
26
Output:
NO
Explanation:
In the first example, 32 is actually 25 so the answer is YES.
The second number is not a power of 2 hence the answer is NO.
Sample Test Cases
Input Output
Test Case 1 22 NO
Test Case 2 16 YES
Test Case 3 82 NO
Test Case 4 512 YES
Test Case 5 1024 YES
Test Case 6 4096 YES
Test Case 7 16384 YES
Test Case 8 65536 YES
Ref: https://www.geeksforgeeks.org/fill-missing-entries-of-a-magic-square/
Find the winner of the Game to Win by erasing any two consecutive similar alphabets
Examples:
Ref: https://www.geeksforgeeks.org/find-the-winner-of-the-game-to-win-by-erasing-any-
two-consecutive-similar-alphabets/
Given an array of N numbers. Two players X and Y play a game where at every step one
player selects a number. One number can be selected only once. After all the numbers have
been selected, player X wins if the absolute difference between the sum of numbers
collected by X and Y is divisible by 4, else Y wins.
Note: Player X starts the game and numbers are selected optimally at every step.
Examples:
Input: a[] = {4, 8, 12, 16}
Output: X
X chooses 4
Y chooses 12
X chooses 8
Y chooses 16
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 which is divisible by 4.
Hence, X wins
Input: a[] = {7, 9, 1}
Output: Y
Ref: https://www.geeksforgeeks.org/predict-the-winner-of-the-game-on-the-basis-of-
absolute-difference-of-sum-by-selecting-numbers/
Minimum Bitwise XOR operations to make any two array elements equal
Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise
XOR operation between any array element and K any number of times. The task is to print
the minimum number of such operations required to make any two elements of the array
equal. If it is not possible to make any two elements of the array equal after performing the
above-mentioned operation then print -1.
Examples:
Input : arr[] = {1, 9, 4, 3}, K = 3
Output :-1
Explanation : No possible to make any two elements equal
Ref: https://www.geeksforgeeks.org/minimum-bitwise-xor-operations-to-make-any-two-
array-elements-equal/