Coding comprises 60% of the total weightage in the InfyTQ examination. If you are thoroughly prepared with the coding part then the chances of cracking the exam increase significantly as the cutoff is 65%.
Practice InfyTQ exam with Edyst Special Test Prep. Pack! Try 1 Free Module!
In this article, we will be discussing some previous coding questions that have been asked in the InfyTQ examination. This will give you an idea of the pattern of the questions asked.
Here are some of theJavacoding questions that have been asked in previous years.
QUESTION 1:
Input: Given a list of numbers separated with a comma.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 List.
Case 2: num2 -> Numbers formed by concatenating all numbers from 5 to 8 in the list
.Output: Sum of num1 and num2
Example: 3,2,6,5,1,4,8,9
Num1: 3+2+6+9 =20
Num2: 5148O/p=5148+20 = 5168
Solution in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Question1 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().split(“, ”);
int num1 = 0;
String num2 = ””;
int n = str.length;
int arr[] = new int[n];
int fiveIndex = -1;
int eightIndex = -1;
for (int i = 0; i < n; i++) //converting string array into Integer array.
{
arr[i] = Integer.parseInt(str[i]);
if (arr[i] == 5) // finding index of 5 in array
fiveIndex = i;
else if (arr[i] == 8)
eightIndex = i; // finding index of 8 in array
}
for (int i = fiveIndex; i <= eightIndex; i++) {
num2 += str[i]; //concatenating all values between 5 and 8 in the array.
arr[i] = 0; //after concatenation we are assigning zero at that index because
//we don’t need that value any more.
}
for (int num: arr) {
num1 += num; //here we are adding all values. we have assigned zero in above loop
//between fiveIndex and eightIndex in the arr.so it will add remaining outer values.
}
System.out.println(num1 + (Integer.parseInt(num2))); //print the sum!
}
}
QUESTION 2:
A string which is a mixture of letters, numbers, and special characters from which produce the largest
even number from the available digit after removing the duplicates digits.
If an even number did not produce then return -1.
Ex: infosys@337
O/p : -1
— — — — — — — — — — — -
Hello#81@21349
O/p : 984312
Solution in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Question2 {
static boolean isDigit(char digit) {
if (digit >= ‘0’ && digit <= ‘9’) return true;
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
StringBuilder sb = new StringBuilder();
HashSet < Integer > hs = new HashSet < > ();
for (int i = 0; i < input.length(); i++) {
if (isDigit(input.charAt(i))) {
String dig = String.valueOf(input.charAt(i));
hs.add(Integer.parseInt(dig));
}
}
ArrayList < Integer > digitList = new ArrayList < > (hs);
Collections.sort(digitList, Collections.reverseOrder());
int len = digitList.size();
boolean evenDigitFound = false;
for (int i = len - 1; i >= 0; i—) {
int digit = digitList.get(i);
if (digit % 2 == 0) {
digitList.remove(i);
digitList.add(digit); //adding even digit at end
evenDigitFound = true;
break;
}
}
if (evenDigitFound == false) {
System.out.print(-1);
} else {
for (int dig: digitList) System.out.print(dig); // printing largest even digits}}}
QUESTION 3:
Take input a number ’N’ and an array as given below.
Input:-N = 2
Array =1,2,3,3,4,4
O/p : 2
Find the least number of unique elements after deleting N numbers of elements from the array.
In the above example, after deleting N=2 elements from the array.
In above 1,2 will be deleted.
So 3,3,4,4 will be remaining so,
2 unique elements are in the array i.e 3 and 4.
Solution in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Question4 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String strArr[] = br.readLine().split(“, ”);
int len = strArr.length;
HashSet < Integer > hs = new HashSet < > ();
for (int i = 0; i < len; i++) {
hs.add(Integer.parseInt(strArr[i]));
}
System.out.println(hs.size() - n);
}
}
QUESTION 4:
A non-empty string containing only alphabets. print the longest prefix from the input string which is the same as the suffix.
Prefix and Suffix should not be overlapped.
Print -1 if no prefix exists which is also the suffix without overlap.
Do case-sensitive comparison.
Positions start from 1.
Input : xxAbcxxAbcxx
o/p: xx (‘xx’ in the prefix and ‘xx’ in the suffix and this is the longest one in the input string so the output will be ‘xx’).
Input: Racecar
o/p: -1 (There is no prefix which is also a suffix so the output will be -1).
Solution in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Question9 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int length = str.length();
int half = length / 2;
for (int i = half; i >= 0; i—) {
String prefix = str.substring(0, i);
String suffix = str.substring(length - i, length);
if (prefix.equals(“”) || suffix.equals(“”)) {
System.out.println(“-1”);
break;
}
if (prefix.equals(suffix)) {
System.out.println(prefix);
break;
}
}
}
}
QUESTION 5:
Number of odd sub-arrays.
Find the number of distinct subarrays in an array of integers such that the sum of the subarray is an odd integer, two subarrays are considered different if they either start or end at different indexes.
Input:
1
3
1 2 3
Output:
4
Explanation : Total subarrays are [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]
In this there are four subarrays which sum is odd i.e: [1],[1,2] ,[2,3],[3].
Solution in Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Question10 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcase = Integer.parseInt(br.readLine());
while (testcase— != 0) {
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(““);
HashSet < String > hs = new HashSet < > ();
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(str[i]);
for (int i = 0; i < n; i++) {
int sum = arr[i];
if (sum % 2 != 0) hs.add(arr[i] + ”“);
StringBuilder sb = new StringBuilder();
sb.append(arr[i]);
for (int j = i + 1; j < n; j++) {
sum += arr[j];
sb.append(arr[j]);
if (sum % 2 != 0) hs.add(sb.toString());
}
}
System.out.println(hs.size()); //that will be the no of unique subarray}}}
The questions asked are of medium difficulty and can be easily solved if you practice well. Practice on Complete Coding Test & Interview Preparation Pack to succeed at Coding rounds at companies like TCS (Ninja), Wipro, Infosys, Mindtree, ValueLabs, CGI, and many more.
When you practise, you get better. It’s very simple.
We hope you find this piece of writing helpful in any way possible.
Wanna drop us some suggestions & ideas to write articles on? Write to us @ team@edyst.com
Enjoyed Reading! Share this blog with your friends...
Sanket Mishra
Sanket is currently pursuing B.tech in Information Technology and loves to solve real-life problems through coding. He loves doing competitive programming & has also worked as a Content Creator Intern in many companies.