InfyTQ Coding Questions 2023
InfyTQ Coding Questions and Solutions
Infosys is conducting its Infosys Competition test through InfyTQ. Here you will get various different coding rounds, out of which the InfyTQ Coding Questions and Solutions round is the easiest one. As you move ahead you get questions of more higher difficulty.
InfyTQ Coding Round Details
Topics | No. of Questions |
---|---|
Number of Questions | 3-5 |
Time Limit | variable |
Difficulty level | high |
Package Offered | 3.6 LPA – 8 LPA |
InfyTQ Practice Coding Questions and Solutions
InfyTQ Practice Coding Questions and Solutions
Question 1: Biggest Meatball
Problem Statement – Bhojon is a restaurant company and has started a new wing in a city. They have every type of cook except the meatball artist. They had fired their last cook because the sale of meatballs in their restaurant is really great, and they can’t afford to make meatballs again and again every time their stock gets empty. They have arranged a hiring program, where you can apply with their meatball.
They will add the meatball in their seekh (a queue) and everytime they cut the meatball they take it and cut it on the day’s quantity and then re-add the meatball in the seekh. You are the hiring manager there and you are going to say who is gonna be hired.
Day’s quantity means, on that very day the company sells only that kg of meatballs to every packet.
If someone has less than a day’s quantity, it will be counted as a sell.
Function Description:
- Complete the function with the following parameters:
Parameters:
Name | Type | Description |
---|---|---|
N | Integer | How many people are participating in the hiring process. |
D | Integer | Day’s quantity, how many grams of meatball is being sold to every packet. |
Array[ ] | Integer array | Array of integers, the weight of meatballs everyone came with. |
Return:
- The ith person whose meat is served at last.
Constraints:
- 1 <= N <= 10^3
- 1 <= D <= 10^3
- 1 <= Array[i] <= 10^3
Input Format:
- First line contains N.
- Second line contains D.
- After that N lines contain The ith person’s meatball weight.
Output Format: The 1 based index of the man whose meatball is served at the last.
Sample Input 1:
4
2
[7 8 9 3]
Sample Output 1:
3
Explanation:
The seekh or meatball queue has [7 8 9 3] this distribution. At the first serving they will cut 2 kgs of meatball from the first meatball and add it to the last of the seekh, so after 1st time it is:
[8 9 3 5]
Then, it is: [9 3 5 6], [3 5 6 7], [5 6 7 1], [6 7 1 3], [7 1 3 4], [1 3 4 5], [3 4 5], [4 5 1], [5 1 2], [1 2 3], [2 3], [3], [1], [0]
So the last served meatball belongs to the 3rd person.
#include<bits/stdc++.h> using namespace std; int main() { int n, x, m = 0, maxPos = 0; cin >> n >> x; vector < int > v(n); for (int i = 0; i < n; i++) { cin >> v[i]; v[i] = (v[i] - 1) / x; if (v[i] >= m) { m = v[i]; maxPos = i; } } cout << maxPos + 1; return 0; }
n = int(input()) m = 0 mxPos = 0 v = [] x = int(input()) v_input = input().split() for i in range(n): v.append(int(v_input[i])) v[i] = (v[i]-1)//x if v[i] >= m: m = v[i] mxPos = i print(mxPos+1)
import java.util.*; class Main { public static void main(String args[]) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int v[]= new int[n]; int x=sc.nextInt(); for(int i=0; i<n; i++) v[i]=sc.nextInt(); for(int i=0;i<n;i++){ v[i]=(v[i]-1)/x; } int m=0, maxPos=0; for(int i=0;i<n;i++){ if(v[i]>=m){ m=v[i]; maxPos=i; } } System.out.println(maxPos+1); } }
Question 2: Self Sufficient
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:
- 1 <= N <= 10^3
- 1 <= EarnArray[i] <= 10^3
- 1 <= CostArray[i] <= 10^3
Input Format:
- First line contains N.
- Second N lines contain The ith earning for the ith book.
- After that N lines contain The cost of the ith book.
Output Format: The minimum money he needs to cover the total expense.
Sample Input 1:
3
[3 4 2]
[5 3 4]
Sample Output 1:
3
Explanation:
At first he buys the 2nd book, which costs 3 rupees, so he saves 1 rupee. Then he buys the 1st book, that takes 2 rupees more. So he spends his stored 1 rupee and hence he needs 1 rupee more. Then he buys the last book.
#include<bits/stdc++.h> using namespace std; int main() { int n, ans = 0, sum = 0; cin >> n; vector< int > arr1(n), arr2(n); for (int i = 0; i < n; i++) { cin >> arr2[i]; } for (int i = 0; i < n; i++) { cin >> arr1[i]; } for (int i = 0; i < n; i++) { arr2[i] -= arr1[i]; } sort(arr2.begin(), arr2.end(), greater< int >()); for (int i = 0; i < n; i++) { sum += arr2[i]; if (sum < 0) { ans += abs(sum); sum = 0; } } cout << ans << endl; return 0; }
n=int(input()) L1=[0] *n L2=[0]*n for i in range(n): L2[i]=int(input()) for i in range(n): L1[i]=int(input()) for i in range(n): L2[i]=L2[i]-L1[i]; L2.sort() su=0 ans=0 for i in range(n): su=su+L2[i] if su<0: ans = ans + abs(su) su=0 print(ans)
import java.util.*; class Main { public static void main(String args[]) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int arr1[]= new int[n]; int arr2[]= new int[n]; for(int i=0; i<n; i++) arr2[i]=sc.nextInt(); for(int i=0; i<n; i++) arr1[i]=sc.nextInt(); for(int i=0; i<n; i++) arr2[i] -= arr1[i]; Arrays.sort(arr2); int sum=0, ans=0; for(int i=n-1; i>=0; i--) { sum+=arr2[i]; if(sum<0) { sum = -sum; ans= ans +sum ; sum=0; } } System.out.println(ans); } }
Question 3: Parallel Columbus
Problem Statement – Nobel Prize-winning Austrian-Irish physicist Erwin Schrödinger developed a machine and brought as many Christopher Columbus from as many parallel universes he could. Actually he was quite amused by the fact that Columbus tried to find India and got America. He planned to dig it further.
Though totally for research purposes, he made a grid of size n X m, and planted some people of America in a position (x,y) [in 1 based indexing of the grid], and then planted you with some of your friends in the (n,m) position of the grid. Now he gathered all the Columbus in 1,1 positions and started a race.
Given the values for n, m, x, y, you have to tell how many different Columbus(s) together will explore you as India for the first time.
Remember, the Columbus who will reach to the people of America, will be thinking that as India and hence wont come further.
Function Description:
Complete the markgame function in the editor below. It has the following parameter(s):
Parameters:
Name | Type | Description |
---|---|---|
n | Integer | The number of rows in the grid. |
m | Integer | The number of columns in the grid. |
x | Integer | The American cell’s Row. |
y | Integer | The American cell’s Column. |
Constraints:
- 1 <= n <= 10^2
- 1 <= m <= 10^2
- 1 <= x <= n
- 1 <= y <= m
Input Format:
- The first line contains an integer, n, denoting the number of rows in the grid.
- The next line contains an integer m, denoting the number of columns in the grid.
- The next line contains an integer, x, denoting the American cell’s row.
- The next line contains an integer, y, denoting the American cell’s column.
Sample Cases
Sample Input 1
2
2
2
1
Sample Output 1
1
Explanation
The only way possible is (1,1) ->(2,1) -> (2,2), so the answer is 1.
#include<bits/stdc++.h> using namespace std; unordered_map<int,long long int> f; long long int Fact(int n) { if(f[n]) return f[n]; return f[n]=n*Fact(n-1); } int main() { int n,m,x,y; cin>>n>>m>>x>>y; n-=1;m-=1;x-=1;y-=1; f[0]=f[1]=1; int p=(Fact(m+n)/(Fact(m)*Fact(n))); int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y)))); cout<<p-imp; }
import math n=int(input())-1 m=int(input())-1 x=int(input())-1 y=int(input())-1 ans=math.factorial(n+m) ans=ans//(math.factorial(n)) ans=ans//(math.factorial(m)) ans1=math.factorial(x+y) ans1=ans1//(math.factorial(x)) ans1=ans1//(math.factorial(y)) x1=n-x y1=m-y ans2=math.factorial(x1+y1) ans2=ans2//(math.factorial(x1)) ans2=ans2//(math.factorial(y1)) print(ans-(ans1*ans2))
import java.util.*; class Main { static int f[] = new int[1000]; static int Fact(int n) { if(f[n]==1) return f[n]; return f[n]=n*Fact(n-1); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int m = sc.nextInt (); int x = sc.nextInt (); int y = sc.nextInt (); n-=1;m-=1;x-=1;y-=1; f[0]=f[1]=1; int p=(Fact(m+n)/(Fact(m)*Fact(n))); int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y)))); System.out.println(p-imp); } }
Question 4 Amusement park
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.
The cost of tickets can be removed by removing the digits from the price given. They will give some k turns to remove the digits from the price of the ticket. Your task is to help Aashay in coding a program that can help him to reduce the cost of a ticket by removing the digits from its price and getting the maximum possible discount.
Note – You cannot make the cost of a ticket zero. For eg -: If the cost of a ticket is 100, and you have 2 turns to reduce the price, the final price will be 1 and not zero.
Constraints:
- 1 <= number of tickets <= 10^5
- 1 <= K <= number of tickets
Input Format for Custom Testing:
- 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
#include<bits/stdc++.h> using namespace std; int smallestNumber (string num, int k) { if(num.length()<=k) return 0; unordered_map<char,int> pos; for(int i=0;i < num.length();i++) { pos[num[i]]=i;} string temp=num; sort(num.begin(),num.end()); string ans=num.substr(0,num.length()-k); vector < int > v; for(int i=0;i < ans.length();i++) v.push_back(pos[ans[i]]); sort(v.begin(),v.end()); string ret; for(int i=0;i < v.size();i++) { ret+=temp[v[i]]; } int final=stoi(ret); return final; } int main() { string s; cin >> s; int k; cin >> k; int ans; cout << smallestNumber(s,k)%(int)(pow(10,9)+7); return 0; }
import sys n=input() k=int(input()) n1=len(n) if len(n)<=k: print(0) sys.exit() a='' i=0 while i < (n1-1) and k>0: if int(n[i])>int(n[i+1]): i+=1 k-=1 continue else: a+=n[i] i+=1 a+=n[i] i+=1 if k>0: a=a[:-k] if i<=(n1-1): while i < n1: a+=n[i] i+=1 print(int(a)%((10**9)+7))
import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); String s = sc.nextLine (); int k = sc.nextInt (); int ans; System.out.println (smallestNumber (s, k) % (int) (1e9 + 7)); } static int smallestNumber (String num, int k) { if (num.length () <= k) return 0; HashMap < Character, Integer > pos = new HashMap <> (); for (int i = 0; i < num.length (); i++) { pos.put (num.charAt (i), i); } String temp = num; num = sortString (num); String ans = num.substring (0, num.length () - k); ArrayList < Integer > v = new ArrayList <> (); for (int i = 0; i < ans.length (); i++) { v.add (pos.get (ans.charAt (i))); } Collections.sort (v); String ret = ""; for (int i = 0; i < v.size (); i++) { ret += temp.charAt (v.get (i)); } int result = Integer.parseInt ("" + ret); return result; } public static String sortString (String inputString) { char tempArray[] = inputString.toCharArray (); Arrays.sort (tempArray); return new String (tempArray); } }
Question 5: 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 10, 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
#include<bits/stdc++.h> using namespace std; int func(int s,int p,int n) { if(p==n-1) return 1; int ans=0; for(int i=s;i<=9;i++) ans+=func(i,p+1,n); return ans; } int main() { int n,a, ans=0; cin>>n; vector < int > v(n); for(int i=0;i<n;i++) { cin>>a; for(int i=0;i<=9;i++) ans+=func(i,0,a); } cout<<ans; }
n=int(input()) a1=[] for i in range(n): a1.append(int(input())) dp=[0]*201 dp[1]=10 dp[2]=55 a=[1]*10 i=3 while i<201: s=0 for i1 in range(10): s+=a[i1] a[i1]=s dp[i]+=(s*(10-i1)) dp[i]=dp[i]%((10**9)+7) i+=1 s1=0 for i in a1: s1+=dp[i] s1=s1%((10**9)+7) print(s1)
import java.util.*; class Main { static int func(int s,int p,int n) { if(p==n-1) return 1; int ans=0; for(int i=s;i<=9;i++) ans += func(i,p+1,n); return ans; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int ans=0; for(int i=0; i<n; i++){ int a = sc.nextInt (); for(int j=0; j <=9; j++) ans+=func(j,0,a); } System.out.println (ans); } }
Question 6: Airport authority
Problem Statement -:
In an airport , the Airport authority decides to charge some minimum amount to the passengers who are carrying luggage with them. They set a threshold weight value, say T, if the luggage exceeds the weight threshold you should pay double the base amount. If it is less than or equal to threshold then you have to pay $1.
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:
- 1 <= N <= 10^5
- 1 <= weights[i] <= 10^5
- 1 <= T <= 10^5
Input Format for Custom Testing:
- The first line contains an integer, N, denoting the number of luggages.
- Each line i of the N subsequent lines (where 0 <= i <n) contains an integer describing weight of ith luggage.
- The next line contains an integer, T, denoting the threshold weight of the boundary wall.
Sample Cases:
- Sample Input 1
4
1
2
3
4
3 - Sample Output 1
5 - Explanation:
Here all weights are less than threshold weight except the luggage with weight 4 (at index 3) so all pays base fare and it pays double fare.
#include <stdio.h> long int weightMachine(long int N,long int weights[],long int T) { long int amount=0,i; for(i=0;i<N;i++) { amount++; if(weights[i]>T) { amount++; } } return amount; } int main() { long int N,i,T; scanf("%ld",&N); long int weights[N]; for(i=0;i<N;i++) { scanf("%ld",&weights[i]); } scanf("%ld",&T); printf("%ld",weightMachine(N,weights,T)); return 0; }
#include <bits/stdc++.h> using namespace std; long int weightMachine(long int N,long int weights[],long int T) { long int amount=0,i; for(i=0;i<N;i++) { amount++; if(weights[i]>T) { amount++; } } return amount; } int main() { long int N,i,T; cin>>N; long int weights[N]; for(i=0;i<N;i++) { cin>>weights[i]; } cin>>T; cout<<weightMachine(N,weights,T); return 0; }
import java.util.*; class Main { static int weightMachine (int N, int weights[],int T) { int amount = 0, i; for (i = 0; i < N; i++) { amount++; if (weights[i] > T) { amount++; } } return amount; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int weights[]= new int[n]; for(int i=0; i<n; i++) weights[i] = sc.nextInt(); int t = sc.nextInt (); System.out.println (weightMachine(n, weights, t)); } }
def weightMachine(N,weights,T): amount=0 for i in weights: amount+=1 if(i>T): amount+=1 return amount N=int(input()) weights=[] for i in range(N): weights.append(int(input())) T=int(input()) print(weightMachine(N,weights,T))
Question 7: Find the homeless
Problem Statement -: There are N Homeless people in the community and N houses in the community. It will be given in the array (people) , height of the person and in the array house capacity of the house is given.
Government decided to give homes for people on the basis of following conditions:
- Priority is given for the people from left to right of the array
- Each person is allotted to a house if and only if the capacity of house is greater than or equal to persons height
- Nearby empty Houses are alloted to the person( starting from extreme left)
You need to find the number of homeless people who have not allotted any home if the government follows the above conditions.So that government will have an idea for how many people they need to allot home for next time.
Constraints:
- 1 <= N <= 10^3
- 1 <= people[i] <= 10^5
- 1 <= house[i] <= 10^5
Input Format for Custom Testing:
- The first line contains an integer, N, denoting the number of people and number of houses.
- Each line i of the N subsequent lines (where 0 <= i <= N) contains an integer describing peoplei.
- Each line i of the N subsequent lines (where 0 <= i <= N) contains an integer describing housei.
Sample Test Cases
- Sample Input 1
3
4
2
7
3
5
10 - Sample Output 1
0 - Explanation
people=[4,2,7]
house=[3,5,10]
People[0] has more priority , from left to right order in houses 5 is the nearest one which fits for people[0]
people[1]=2 will fit in 3 which is nearer from left
people[2]=7 will fit in remaining house of capacity of 10
So no homeless people left so return 0
- Sample Input 2
3
3
8
5
1
9
4 - Sample Output 2
2 - Explanation
people=[3,8,5]
house=[1,9,4]
people[0]=3 can fit in 9 which is nearest from left in array house
people[1]=8 cannot fit in any home which is left (i.e, 1 and 4)
people[2]=5 cannot fit in any home which is left (i.e, 1 and 4)
So return 2,which is number of homeless people
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int people[n], house[n]; for(int i=0; i<n; i++) cin>>people[i]; for(int i=0; i<n; i++) cin>>house[i]; int count = 0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(people[i]<house[j]){ count+=1 ; house[j]=-1; break ; } } } cout<<n-count; }
import java.util.*; class Main { public static void main(String args[]) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int people[]= new int[n]; int house[]= new int[n]; for(int i=0; i<n; i++) people[i]=sc.nextInt(); for(int i=0; i<n; i++) house[i]=sc.nextInt(); int count = 0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(people[i]<house[j]){ count+=1 ; house[j]=-1; break ; } } } System.out.println(n-count); } }
N=int(input()) people=[] house=[] #to read data of people and house arrays for i in range(N): people.append(int(input())) for i in range(N): house.append(int(input())) count=0 for i in range(N): for j in range(N): if(people[i] < house[j]): count+=1 house[j]=-1 break print(N-count)
Thank you for the questions. These helped me a lot in my preparations
What is the most important thing to learn to score good in InfyTQ?
I really like the solutions.Thank you.
me too. I like the solution too.
Can you please tell me more about the certification process.
Thanku for such awesome Questions
thank you very much sie/mam,simple and easy code….. i like to python