Infosys Off Campus Coding Question and Answers 2024, Get Infosys Interview Coding Questions

Infosys Off Campus Coding Question and Answers 2024 | Infosys Interview Coding Questions | Infosys Off Campus Interview Questions and Answers 

Infosys Off Campus Coding Question and Answers 2024: All of the fundamental algorithms, data structures, and aptitude concepts are included in the Infosys Off Campus Coding Interview Questions. We can assist you if you’re interested in working at Infosys but are unsure about how to get ready for the questions in the Infosys Off Campus Coding Interview Round. One of the most important sections of the Infosys Off Campus Interview exam is the coding round. In this article, we covered a variety of sample Infosys Off campus Coding Questions and Answers 2024. We will talk about Infosys Off Campus Interview Coding Questions 2024/Infosys Interview Questions in the below section.

This tutorial will teach you all there is to know about Infosys Off Campus Interview Questions PDF/ Infosys Off Campus Drive Interview Questions. More details like Infosys Off Campus Drive Interview Coding Questions and Answer, Infosys Off Campus Coding Questions and Answers, Off Campus Coding Questions and Answers 2024 Infosys, Cut Off, Salary, Hike, Increment, Annual Appraisal, etc. are available on this page.

Infosys Off Campus Interview Coding Questions 2024

Company Name Infosys
Category Coding Questions and Answers

Infosys Coding Interview and Answers 

Question 1: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you’ll need to defeat in this quest. Each monster i is described with two integer numbers – poweri and bonusi. To defeat this monster, you’ll need at least poweri experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonusi experience points if you defeat this monster. You can defeat monsters in any order.

The quest turned out to be very hard – you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you’re interested, what is the maximum possible number of monsters you can defeat?

Input:

  • The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience.
  • Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, poweri, which represents power of the corresponding monster.
  • Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonusi, which represents bonus for defeating the corresponding monster.
Input Output Output Description
2
123
78
130
10
0
2
  • Initial experience level is 123 points.
  • Defeat the first monster having power of 78 and bonus of 10. Experience level is now 123+10=133.
  • Defeat the second monster.
3
100
101
100
304
100
1
524
2
  • Initial experience level is 100 points.
  • Defeat the second monster having power of 100 and bonus of 1. Experience level is now 100+1=101.
  • Defeat the first monster having power of 101 and bonus of 100. Experience level is now 101+100=201.
  • The third monster can’t be defeated.

JAVA

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int exp = s.nextInt();

        int monst[] = new int[n];
        int bonus[] = new int[n];

        for (int i = 0; i < n; i++) {
            monst[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++) {
            bonus[i] = s.nextInt();
        }

        class Monster {
            private final int power, bonus;
            public Monster(int power, int bonus){
                this.power = power;
                this.bonus = bonus;
            }
        }

        Monster[] monsters = new Monster[n];

        for(int i = 0; i < n; i++)
            monsters[i] = new Monster(monst[i], bonus[i]);

        Arrays.sort(monsters, Comparator.comparingInt(m -> m.power));

        int count = 0;

        for(Monster m: monsters){
            if(exp < m.power) break;
            exp += m.bonus;
            ++count;
        }
        System.out.println(count);
    }
}

PHYTHON

n = int(input())
lev = int(input())
p = []
b = []
a = []
ans = 0
for i in range(n):
   p.append(int(input()))
for j in range(n):
   b.append(int(input()))
for k in range(n):
   a.append([p[k], b[k]])
a.sort()
for z in a:
   if z[0] > lev:
       break
   lev += z[1]
   ans += 1
print(ans)

C++

#include<iostream>
#include<map>









using namespace std;
int main(){
int nMon,nExp;
cin>>nMon;
cin>>nExp;
map<int,int> monster;
int arr[nMon],monBon;
for(int i=0; i<nMon; i++){ cin>>arr[i];
}
for(int i=0; i<nMon; i++){ cin>>monBon;
    monster[arr[i]]=monBon;
}

map<int,int> :: iterator iter;

int value,valBon,counter=0;;

for(iter=monster.begin(); iter!= monster.end(); iter++){

    value=(*iter).first;

    valBon=(*iter).second;

     if(nExp>=value){

        nExp=nExp+valBon;

        counter++;

     }else{

        break;

     }

}

cout<<"OUTPUT : "<< counter<<endl;

}

 

 

Question 2: You have an array of integers A1 A2 .. An. Find the longest increasing subsequence Ai1 Ai2 .. Ak
(1 <= k <= N) that satisfies the following condition:
For every adjacent pair of numbers of the chosen subsequence Ai[x] and Ai[x+1] (1 < x < k), the expression( Ai[x] Ai[x+1] ) * 2 < ( Ai[x] Ai[x+1] ) is true

Note: ‘&’ is the bitwise AND operation, ‘ | ‘ is the bit-wise OR operation

Input:

  1. The first line contains an integer, N, denoting the number of elements in A.
  2. Each line i of the N subsequent lines (where 0 ≤ i < N) contains an integer describing Ai.

Sample cases:

Input Output Output Description
5
15
6
5
12
1
2 One possible subsequence is: 5 12
6
9
17
2
15
5
2
2 One possible subsequence is: 2 15
7
17
16
12
2
8
17
17
3 One possible subsequence is: 2 8 17

JAVA 

class Main
{
    public static int LIS(int[] arr, int i, int n, int prev)
    {
        if (i == n) {
            return 0;
        }
        int excl = LIS(arr, i + 1, n, prev);

        int incl = 0;
        if (arr[i] > prev) {
            incl = 1 + LIS(arr, i + 1, n, arr[i]);
        }

        return Integer.max(incl, excl);
    }

    public static void main(String[] args)
    {
        int[] arr = { 15, 6, 5, 12, 1 };

        System.out.print("The length of the LIS is "
                        + LIS(arr, 0, arr.length, Integer.MIN_VALUE));
    }
}

PHYTHON

def sub(arr, i, n, prev=0):
   if i == n:
       return 0
   a = sub(arr, i + 1, n, prev)
   b = 0
   if arr[i] > prev:
       b = 1 + sub(arr, i + 1, n, arr[i])
   return max(b, a)


n = int(input())
arr = []
for i in range(n):
   arr.append(int(input()))
print("Length of Bitwise subsequence will be", sub(arr, 0, len(arr)))

C++

#include<bits/stdc++.h>
using namespace std;
int sub(int arr[], int i, int n, int prev){
    if(i == n)
       return 0;
    int a = sub(arr, i + 1, n, prev);
    int b = 0;
    if (arr[i] > prev)
       b = 1 + sub(arr, i + 1, n, arr[i]);
   
    return max(b, a);
}

int main(){
    
    int n;
    cin>>n;
    
    int arr[n];
    
    for(int i=0; i< n; i++)
    cin>>arr[i];
    
    int res = sub(arr, 0, n, 0);
    cout<< res;
}

Question 3: You have been given a string S of length N. The given string is a binary string which consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal   number that this binary string represents. Example:

  • “101” represents 5.
  • “0000” represents 0.
  • “01010” represents 10.

There are two types of operations that can be performed on the given string.

  • Swap any two characters by paying a cost of A coins.
  • Flip any character by paying a cost of B coins
  • flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the ugliness of the string by performing the above mentioned operations on it. Since the answer can be very large, return the answer modulo 10^9+7.

Note:

  • You can perform an operation only if you have enough number of coins to perform it.
  • After every operation the number of coins get deducted by the cost for that operation.

Input Format

  • The first line contains an integer, N, denoting the number of character in the string
  • The next line contains a string, S, denoting the the binary string
  • The next line contains an integer, CASH, denoting the total number of coins present initially
  • Next will contains an integer, A, denoting the cost to swap two characters.
  • Then the next line contains an integer, B, denoting the cost to flip a character.

Constraints

  • 1 <= N <= 10^5
  • 1< len(S)<= 10^5
  • 1<=CASH <=10^5
  • 1<=A<=10^5
  • 1<=B<=10^5

Sample Input 1 :

4
1111
7
1
2

  Sample Output 1 :

1

  Explanation:

3 flips can be used to create “0001” which represents 1.

  Sample Input 2:

6
111011
7
1
3

  Sample Output 2:

7

  Explanation:

First swap 0 with the most significant 1, then use flip twice first on index one and then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

  Sample Input 3:

6
111011
7
3
2

  Sample Output 3:

3

 Explanation:

Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N

JAVA
import java.util.*;
class Main
{
  static String str;
  static int cash, n, a, b;
  static void swapf ()
  {
    char s[] = str.toCharArray ();
    int i = 0;
    for (int a = 0; a < s.length; a++)
      if (s[a] == '1')
	{
	  i = a;
	  break;
	}
    int j = s.length - 1;
    while (j > i)
      {
	if (cash < a)
	  break;
	if (s[j] == '0')
	  {
	    if (s[i] == '0')
	      i++;
	    else
	      {
		char temp = s[i];
		s[i] = s[j];
		s[j] = temp;
		cash -= a;
		j--;
	      }
	  }
	else
	  j--;
      }
    str = new String (s);
  }
  static void flipf ()
  {
    char s[] = str.toCharArray ();
    int i = 0;

    for (int a = 0; a < s.length; a++)
      if (s[a] == '1')
	{
	  i = a;
	  break;
	}
    while (cash >= b)
      {
	if (i == s.length)
	  break;
	if (s[i] == '1')
	  {
	    s[i] = '0';
	    i++;
	    cash -= b;
	  }
      }
    str = new String (s);
  }

  public static void main (String[]args)
  {
    Scanner sc = new Scanner (System.in);
    n = sc.nextInt ();
    str = sc.next ();
    cash = sc.nextInt ();
    a = sc.nextInt ();
    b = sc.nextInt ();

    if (a < b)
      {
	swapf ();
	flipf ();
      }
    else
      {
	flipf ();
	swapf ();
      }
    System.out.println (Integer.parseInt (str, 2));
  }
}

PHYTHON 

# N = int(input())
# S = list(input())
# Cash = int(input())
# A = int(input())
# B = int(input())
N = 6
S = list("111011")
Cash = 7
A = 1
B = 3


def swap():
   global Cash
   Rs = S.copy()
   S[S.index('1')], S[''.join(S).rindex('0')] = S[''.join(S).rindex('0')], S[S.index('1')]
   if Rs == S:
       flip()
   else:
       Cash -= A


def flip():
   global Cash
   S[S.index('1')] = '0'
   Cash -= B


while Cash > A or Cash > B:
   if A < B and '0' in S:
       swap()
   else:
       flip()
print(int(''.join(S), 2))

C++
#include<bits/stdc++.h>
using namespace std;
string s;
int n, cash, a, b;

void swapf ()
{
  int i;

  for (int a = 0; a < s.length (); a++) 
  if (s[a] == '1') 
  { 
      i = a; 
      break; 
      
  } 
  int j = s.length () - 1; 
  while (j > i)
    {
      if (cash < a)
	break;

      if (s[j] == '0')
	{
	  if (s[i] == '0')
	    i++;

	  else
	    {
	      swap (s[i], s[j]);
	      cash -= a;
	      j--;
	    }
	}
      else
	j--;
    }
}
void flipf ()
{
  int i;
  for (int a = 0; a < s.length (); a++) 
  if (s[a] == '1') 
  { 
      i = a; break; 
      
  } 
  while (cash >= b)
    {

      if (i == s.length ())
      break;

      if (s[i] == '1')
	{
	  s[i] = '0';
	  i++;
	  cash -= b;
	}
    }
}

int main ()
{
  cin >> n >> s >> cash >> a >> b;

  if (a < b)
    {
      swapf ();
      flipf ();
    }

  else
    {
      flipf ();
      swapf ();
    }

  cout << stoull (s, 0, 2);

}

Question 4: Khaled has an array A of N elements. It is guaranteed that N is even. He wants to choose at most N/2 elements from array A. It is not necessary to choose consecutive elements.  Khaled is interested in XOR of all the elements he chooses. Here, XOR denotes the bitwise XOR operation.

For example:

  • If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR 4 XOR 8)=14.

Khaled wants to maximize the XOR of all the elements he chooses. Your task is to help khaled to find the max XOR of a subset that he can achieve by choosing at most N/2 elements?

   Input format:

  • The first line contains an integer, N, denoting the number of elements in A.
  • Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

   Constraints 

  • 1<=N<=120
  • 1<=A[i]<=10^6

   Sample Input 1

2
1
2
   Sample Output 1
2

Explanation:

N=2,  A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset is 2. And the number of elements in the subset is 1 which is less than N/2.

Sample Input 2
4
1
2
4
7

Sample Output 2

7

Explanation:

N=4,  A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the subset is 7, and the number of elements in the subset is 1 which is less than N/2.

JAVA

import java.util.*;
class Main
{
  public static void main (String[]args)
  {
    final int N = 120, M = 1 << 20;
    int dp[] = new int[M];
    char res[] = new char[M];

    Scanner sc = new Scanner (System.in);
    int n = sc.nextInt ();
    int arr[] = new int[n];

    for (int i = 0; i < n; i++)
        arr[i] = sc.nextInt ();

    for (int i = 1; i < M; i++)
        dp[i] = Integer.MAX_VALUE;

    for (int i = 0; i < n; ++i)
      {
	if (arr[i] == 0)
	  continue;
	for (int j = 0; j < M; ++j)
	  res[j] = 0;

	for (int k = 0; k < M; ++k) { if (res[k] == 1) continue; if (dp[k] > dp[k ^ arr[i]])
	      dp[k] = dp[k ^ arr[i]] + 1;
	      
	    else if (dp[k ^ arr[i]] > dp[k])
	      dp[k ^ arr[i]] = dp[k] + 1;

	    res[k ^ arr[i]] = 1;
	  }
      }

    int j = M - 1, k = n >> 1;
    while (dp[j] > k)
      --j;
    System.out.println (j);
  }
}

PHYTHON

from itertools import combinations


def fun(arr, N):
   sub = []
   max_xor = max(arr)
   for i in range(1, N // 2):
       comb = combinations(arr, i + 1)
       for i in comb:
           sub.append(list(i))
   for a in sub:
       z = 0
       for b in a:
           z = z ^ b
       if z > max_xor:
           max_xor = z
   return max_xor


N = int(input("Enter Length : "))
arr = []
for i in range(N):
   arr.append(int(input(f"Enter {i+1} Element : ")))
if N < 3:
   print("Output :", max(arr))
else:
   print("Output :", fun(arr, N))

C++

#include<bits/stdc++.h>
using namespace std;

int main ()
{
  int n;
  cin >> n;
  
  int arr[n];
  
  for (int i = 0; i < n; i++) cin >> arr[i];
  
  int M = 1 << 20;
  int dp[M];
  char res[M];
  
  for (int i = 1; i < M; i++)
    dp[i] = INT_MAX;
  
  for (int i = 0; i < n; i++)
    {
      if (arr[i] == 0)
	   continue;
      
      for (int j = 0; j < M; j++)
	    res[j] = 0;
      
      for (int k = 0; k < M; k++) { if (res[k] == 1) continue; if (dp[k] > dp[k ^ arr[i]])
	       dp[k] = dp[k ^ arr[i]] + 1;
	  
         else if (dp[k ^ arr[i]] > dp[k])
	       dp[k ^ arr[i]] = dp[k] + 1;
	     
	     res[k ^ arr[i]] = 1;
	      
	  }
    }
  
 int j = M - 1, k = n >> 1;
  
  while (dp[j] > k)
    j--;
  
  cout << j;
  
  return 0;

}

Question 5: One of the first lessons IT students learn is the representation of natural numbers in the binary number system (base 2) This system uses only two digits, 0 and 1. In everyday life we use for convenience the decimal system (base 10) which uses ten digits, from 0 to 9. In general, we could use any numbering system.Computer scientists often use systems based on 8 or 16. The numbering system based on K uses K digits with a value from 0 to K-1. Suppose a natural number M is given, written in the decimal system To convert it to the corresponding writing in the system based on K, we successively divide M by K until we reach a quotient that is less than K

The representation of M in the system based on K is formed by the final quotient (as first digit) and is followed by the remainder of the previous divisionsFor example :

  •  If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
  • In decimal system = 172 in octal system.
  • 172 in base 8 = 1*8^2 + 7*8 + 2 = 122

You made the following observation in applying the above rule of converting natural numbers to another numbering system

  •  In some cases in the new representation all the digits of the number are the same. For example 63 in base 10= 333 in base 4

Given a number M in its decimal representation, your task is find the minimum base B such that in the representation of M at base B all digits are the same.

Input Format

  • The first line contains an integer, M, denoting the number given

Constraints

  • 1 <= M = 10^12

Sample Input 1 :

41

Sample Output 1 :

40

Explanation :

Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller base satisfying the requirements

Sample Input 2 :

34430

Sample Output 2 :

312

Explanation :

Here 34430 in base 312 will have all digits the same and there is no smaller base satisfying the requirement

JAVA 

import java.util.*;
class Main
{
  public static boolean convertBase (int m, int base)
  {

    int rem = m % base;
      m = m / base;

    while (m >= base && (m % base == rem))
      m = m / base;

    if (m == rem)
      return true;

      return false;
  }

  public static void main (String[]args)
  {
    Scanner sc = new Scanner (System.in);
    int m = sc.nextInt ();
    int base = 2;

    while (convertBase (m, base) != true)
      base++;

    System.out.println (base);

  }

}

PHYTHON

def convertBase(m, base):
   rem = m % base
   m = m // base
   while m >= base and (m % base == rem):
       m = m // base
   if m == rem:
       return True
   return False


m = int(input())
base = 2
while not convertBase(m, base):
   base = base + 1
print(base)

C++

#include<bits/stdc++.h>
using namespace std;


bool converted (int M, int base)
{
  int rem = M % base;
  M /= base;

  while (M >= base)
    {
      if (M % base != rem)
	return 0;

      M /= base;
    }

  if (M == rem)
    return 1;

  return 0;
}

int main ()
{
  int M;
  cin >> M;

  int base = 2;

  while (converted (M, base) != 1)
    {
      base++;
    }

  cout << base;

  return 0;
}

Keep watch dailyrecruitment.in site for more upcoming exams notifications.

INSTAGRAM LINK FOLLOW NOW >>
JOB ALERT ON TELEGRAM JOIN NOW>>

 

Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now