Top Infosys Coding Questions and Answers
Top Infosys Coding Questions and Answers
Answers (2024)
om
Answer:
Sample Input
.c
B[ ] = {25, 26, 27, 28}
er
Sample Output
#include <iostream>
using namespace std;
b[i] = temp;
}
}
int main() {
Cc
swapArrays(a, b, n);
Output
om
Q.2 - Write a program to arrange the given numbers to form the
biggest number
Answer:
.c
Sample Input
er
{5, 67, 2, 88, 9, 76, 52, 4}
rn
C++
#include <iostream>
#include <vector>
ea
#include <algorithm>
using namespace std;
int main() {
Cc
Java
import java.util.*;
om
public int compare(String a, String b) {
// Compare two concatenations to decide which should come first
return (b + a).compareTo(a + b);
}
});
.c
// Check for a case where the largest number is 0
if (numbers[0].equals("0")) {
System.out.println("0");
er
return;
}
Python
od
def largestNumber(arr):
# Convert integers to strings to enable custom sorting
arr = sorted(map(str, arr), key=lambda x: x*10, reverse=True)
# Join and return the sorted array, handling leading zeros
Cc
result = ''.join(arr)
return result if result[0] != '0' else '0'
Output
988766752452
Q.3 - Find the smallest and largest number in an Array
Answer:
Sample Input
om
C++ Code:
#include <iostream>
#include <vector>
using namespace std;
.c
int main() {
vector<int> arr = {3, 1, 56, 34, 12, 9, 98, 23, 4};
er
int minVal = arr[0];
int maxVal = arr[0];
return 0;
el
Java Code:
od
Output
om
Smallest Number: 1
Largest Number: 98
.c
Q.4 - Find the next permutation of the given string in C++
er
Answer:
Sample Input
rn
s=”dcd”
C++ Code:
ea
#include <iostream>
#include <algorithm>
using namespace std;
el
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
Cc
bool nextPermutation(string& s) {
int n = s.length();
int i = n - 2;
while (i >= 0 && s[i] >= s[i + 1])
i--;
if (i < 0)
return false;
int j = n - 1;
while (s[j] <= s[i])
j--;
swap(s[i], s[j]);
reverseString(s, i + 1, n - 1);
om
return true;
}
int main() {
string s = "dcd"; // Sample Input
bool val = nextPermutation(s);
.c
if (!val)
cout << "No next permutation possible" << endl;
else
er
cout << "Next permutation: " << s << endl;
return 0;
}
rn
Output
Answer:
Sample Input:
od
123
456
789
Cc
C++ Code:
#include <iostream>
#include <vector>
using namespace std;
om
left++;
right--;
}
}
}
.c
int main() {
// Input matrix
vector<vector<int>> matrix = {{1, 2, 3},
er
{4, 5, 6},
{7, 8, 9}};
}
cout << endl;
}
el
// Rotate matrix
rotateMatrix(matrix);
od
}
cout << endl;
}
return 0;
}
Java Code:
import java.util.Arrays;
public class RotateMatrix {
public static void rotateMatrix(int[][] matrix) {
int n = matrix.length;
// Transpose the matrix
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
om
}
// Reverse each row
for (int i = 0; i < n; ++i) {
int left = 0, right = n - 1;
while (left < right) {
int temp = matrix[i][left];
.c
matrix[i][left] = matrix[i][right];
matrix[i][right] = temp;
left++;
er
right--;
}
}
}
rn
public static void main(String[] args) {
// Input matrix
int[][] matrix = {{1, 2, 3},
ea
{4, 5, 6},
{7, 8, 9}};
System.out.println("Original Matrix:");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
od
// Rotate matrix
rotateMatrix(matrix);
Cc
Python Code:
def rotate_matrix(matrix):
n = len(matrix)
# Transpose the matrix
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reverse each row
for i in range(n):
matrix[i] = matrix[i][::-1]
# Input matrix
om
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
.c
for row in matrix:
print(row)
er
# Rotate matrix
rotate_matrix(matrix)
Output:
Rotated Matrix:
el
741
852
od
963
Q.6 How do you find the missing characters to make a string pangram?
Write a program
Answer:
Sample Input: "A quick movement of the enemy will jeopardize six gunboats"
C++ Code:
#include <iostream>
#include <string>
#include <unordered_set>
#include <algorithm>
om
}
for (char c : s) {
alphabets.erase(tolower(c));
}
.c
std::string missing;
for (char c : alphabets) {
er
missing.push_back(c);
}
std::sort(missing.begin(), missing.end());
rn
return missing;
}
int main() {
ea
std::string str = "A quick movement of the enemy will jeopardize six gunboats";
std::cout << "Missing characters: " << findMissingCharacters(str) << std::endl;
return 0;
}
el
Java Code:
import java.util.HashSet;
od
import java.util.Set;
return missing.toString();
}
om
}
Python Code:
def find_missing_characters_for_pangram(s):
alphabets = set('abcdefghijklmnopqrstuvwxyz')
for char in s.lower():
.c
alphabets.discard(char)
return ''.join(sorted(alphabets))
er
# Example usage
string = "A quick movement of the enemy will jeopardize six gunboats"
missing_characters = find_missing_characters_for_pangram(string)
rn
print("Missing characters:", missing_characters)
Output
ea
Answer:
C++ Code:
#include <iostream>
#include <unordered_set>
int main() {
std::string input = "Hello, World!";
std::cout << "Number of unique characters: " << countUniqueCharacters(input) <<
std::endl;
return 0;
}
Java Code:
om
import java.util.HashSet;
import java.util.Set;
.c
Set<Character> uniqueChars = new HashSet<>();
for (char c : str.toCharArray()) {
er
uniqueChars.add(c);
}
return uniqueChars.size();
}
rn
public static void main(String[] args) {
String input = "Hello, World!";
System.out.println("Number of unique characters: " + countUniqueCharacters(input));
ea
}
}
Python Code:
el
def count_unique_characters(s):
return len(set(s))
od
# Example usage
input_string = "Hello, World!"
unique_character_count = count_unique_characters(input_string)
print("Number of unique characters:", unique_character_count)
Cc
Output
a)
12
34
b)
43
21
om
C++ Code:
#include <iostream>
#include <vector>
.c
std::vector<std::vector<int>> subtractMatrices(const std::vector<std::vector<int>>& A, const
std::vector<std::vector<int>>& B) {
er
std::vector<std::vector<int>> result(A.size(), std::vector<int>(A[0].size()));
for (size_t i = 0; i < A.size(); ++i) {
for (size_t j = 0; j < A[0].size(); ++j) {
result[i][j] = A[i][j] - B[i][j];
rn
}
}
return result;
}
ea
int main() {
std::vector<std::vector<int>> A = {{1, 2}, {3, 4}};
std::vector<std::vector<int>> B = {{4, 3}, {2, 1}};
std::vector<std::vector<int>> result = subtractMatrices(A, B);
el
Java Code:
om
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
.c
}
Python Code:
er
def subtract_matrices(A, B):
return [[A[i][j] - B[i][j] for j in range(len(A[0]))] for i in range(len(A))]
rn
# Example usage
A = [[1, 2], [3, 4]]
B = [[4, 3], [2, 1]]
result = subtract_matrices(A, B)
ea
Output
-3 -1
od
1 3
Q.9 How do you multiply two matrices and show results through
Cc
A:
12
34
B:
56
78
C++ Code:
#include <iostream>
#include <vector>
om
size_t cols = B[0].size();
size_t common = B.size();
std::vector<std::vector<int>> result(rows, std::vector<int>(cols, 0));
.c
for (size_t k = 0; k < common; ++k) {
result[i][j] += A[i][k] * B[k][j];
}
er
}
}
return result;
rn
}
int main() {
std::vector<std::vector<int>> A = {{1, 2}, {3, 4}};
ea
}
return 0;
}
Java Code:
Cc
om
int[][] result = multiplyMatrices(A, B);
.c
System.out.println();
}
}
er
}
Python Code:
rn
def multiply_matrices(A, B):
rows_A, cols_A = len(A), len(A[0])
rows_B, cols_B = len(B), len(B[0])
result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]
ea
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
el
# Example usage
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
result = multiply_matrices(A, B)
Cc
Output
19 22
43 50
Q.10 How do you convert decimal numbers to binary numbers? Write
a Program
Answer:
Sample Input: 29
C++ Code
#include <iostream>
om
#include <string>
#include <algorithm>
std::string decimalToBinary(int n) {
std::string binary = "";
while (n > 0) {
.c
binary += std::to_string(n % 2);
n /= 2;
er
}
std::reverse(binary.begin(), binary.end());
return binary;
}
rn
int main() {
int decimal = 29;
std::cout << "Binary of " << decimal << " is " << decimalToBinary(decimal) << std::endl;
ea
return 0;
}
Java Code:
el
while (n > 0) {
binary.insert(0, n % 2);
n /= 2;
}
Cc
return binary.toString();
}
Python Code:
def decimal_to_binary(n):
binary = ""
while n > 0:
binary = str(n % 2) + binary
n //= 2
return binary
# Example usage
decimal = 29
binary = decimal_to_binary(decimal)
om
print(f"Binary of {decimal} is {binary}")
Output
.c
er
Follow Us on Facebook for Latest Updates.
rn
ea
el
od
Cc