Skip to content

Infosys Coding Questions with Solutions 2025 - 100+ Problems in C, C++, Java, Python

Practice 100+ Infosys coding questions with detailed solutions in C, C++, Java, and Python. Previous year IRT coding problems, difficulty levels, topic-wise organization, and step-by-step explanations for Infosys placement preparation.

This comprehensive collection contains 100+ Infosys coding questions from previous year IRT exams with detailed solutions in multiple programming languages. Practice these problems to master the Infosys coding section.

ParameterDetails
Total Problems2 problems
Time Allocated60-70 minutes
Marks20 marks (10 marks per problem)
DifficultyEasy-Medium and Medium-Hard
Languages AllowedC, C++, Java, Python
PlatformOnline compiler with test cases

Q1: Find Second Largest Find the second largest element in an array.

Solution (C):

#include <stdio.h>
#include <limits.h>
int secondLargest(int arr[], int n) {
int first = INT_MIN, second = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
return second;
}

Solution (Java):

public int secondLargest(int[] arr) {
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return second;
}

Solution (Python):

def second_largest(arr):
first = second = float('-inf')
for num in arr:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
return second

Time Complexity: O(n) | Space Complexity: O(1)

Q2: Reverse Array Reverse an array without using extra space.

Solution (C):

void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

Solution (Java):

public void reverseArray(int[] arr) {
int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

Solution (Python):

def reverse_array(arr):
start, end = 0, len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1

Time Complexity: O(n) | Space Complexity: O(1)

Q3: Check Palindrome String Check if a string is palindrome (case-insensitive).

Solution (C):

#include <ctype.h>
#include <string.h>
int isPalindrome(char str[]) {
int left = 0, right = strlen(str) - 1;
while (left < right) {
if (tolower(str[left]) != tolower(str[right]))
return 0;
left++;
right--;
}
return 1;
}

Solution (Java):

public boolean isPalindrome(String str) {
str = str.toLowerCase();
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right))
return false;
left++;
right--;
}
return true;
}

Solution (Python):

def is_palindrome(s):
s = s.lower()
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True

Time Complexity: O(n) | Space Complexity: O(1)

Q4: Remove Duplicates Remove duplicates from sorted array, return new length.

Solution (C):

int removeDuplicates(int arr[], int n) {
if (n == 0) return 0;
int j = 0;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}

Solution (Java):

public int removeDuplicates(int[] arr) {
if (arr.length == 0) return 0;
int j = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}

Solution (Python):

def remove_duplicates(arr):
if not arr:
return 0
j = 0
for i in range(1, len(arr)):
if arr[i] != arr[j]:
j += 1
arr[j] = arr[i]
return j + 1

Time Complexity: O(n) | Space Complexity: O(1)

Q5: Prime Number Check Check if a number is prime.

Solution (C):

int isPrime(int n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
return 1;
}

Solution (Java):

public boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}

Solution (Python):

def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True

Time Complexity: O(√n) | Space Complexity: O(1)

Q6: GCD of Two Numbers Find GCD (Greatest Common Divisor) of two numbers.

Solution (C):

int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

Solution (Java):

public int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

Solution (Python):

def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)

Time Complexity: O(log(min(a,b))) | Space Complexity: O(log(min(a,b))))

Q7: Factorial Find factorial of a number.

Solution (C):

long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

Solution (Java):

public long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

Solution (Python):

def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)

Time Complexity: O(n) | Space Complexity: O(n)

Q8: Fibonacci Series Print Fibonacci series up to n terms.

Solution (C):

void fibonacci(int n) {
if (n <= 0) return;
int a = 0, b = 1;
printf("%d ", a);
if (n > 1) printf("%d ", b);
for (int i = 2; i < n; i++) {
int c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}

Solution (Java):

public void fibonacci(int n) {
if (n <= 0) return;
int a = 0, b = 1;
System.out.print(a + " ");
if (n > 1) System.out.print(b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}

Solution (Python):

def fibonacci(n):
if n <= 0:
return
a, b = 0, 1
print(a, end=" ")
if n > 1:
print(b, end=" ")
for i in range(2, n):
c = a + b
print(c, end=" ")
a, b = b, c

Time Complexity: O(n) | Space Complexity: O(1)

Q9: Anagram Check Check if two strings are anagrams.

Solution (C):

#include <string.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return *(char*)a - *(char*)b;
}
int isAnagram(char s1[], char s2[]) {
int len1 = strlen(s1), len2 = strlen(s2);
if (len1 != len2) return 0;
qsort(s1, len1, sizeof(char), compare);
qsort(s2, len2, sizeof(char), compare);
return strcmp(s1, s2) == 0;
}

Solution (Java):

public boolean isAnagram(String s1, String s2) {
if (s1.length() != s2.length()) return false;
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}

Solution (Python):

def is_anagram(s1, s2):
if len(s1) != len(s2):
return False
return sorted(s1) == sorted(s2)

Time Complexity: O(n log n) | Space Complexity: O(1)

Q10: Reverse Words Reverse words in a string.

Solution (C):

#include <string.h>
#include <stdio.h>
void reverseWords(char str[]) {
int start = 0, end = strlen(str) - 1;
// Reverse entire string
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
// Reverse each word
start = 0;
for (int i = 0; i <= strlen(str); i++) {
if (str[i] == ' ' || str[i] == '\0') {
end = i - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
start = i + 1;
}
}
}

Solution (Java):

public String reverseWords(String str) {
String[] words = str.split(" ");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]);
if (i > 0) result.append(" ");
}
return result.toString();
}

Solution (Python):

def reverse_words(s):
words = s.split()
return ' '.join(reversed(words))

Time Complexity: O(n) | Space Complexity: O(n)

Q11: Count Vowels Count vowels in a string.

Solution (C):

#include <ctype.h>
int countVowels(char str[]) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
char c = tolower(str[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
return count;
}

Solution (Java):

public int countVowels(String str) {
int count = 0;
String vowels = "aeiouAEIOU";
for (char c : str.toCharArray()) {
if (vowels.indexOf(c) != -1)
count++;
}
return count;
}

Solution (Python):

def count_vowels(s):
vowels = "aeiouAEIOU"
return sum(1 for c in s if c in vowels)

Time Complexity: O(n) | Space Complexity: O(1)

Q12: Maximum Subarray Sum Find maximum sum of contiguous subarray (Kadane’s Algorithm).

Solution (C):

int maxSubArraySum(int arr[], int n) {
int max_sum = arr[0];
int current_sum = arr[0];
for (int i = 1; i < n; i++) {
current_sum = (arr[i] > current_sum + arr[i]) ?
arr[i] : current_sum + arr[i];
max_sum = (current_sum > max_sum) ? current_sum : max_sum;
}
return max_sum;
}

Solution (Java):

public int maxSubArraySum(int[] arr) {
int maxSum = arr[0];
int currentSum = arr[0];
for (int i = 1; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}

Solution (Python):

def max_subarray_sum(arr):
max_sum = current_sum = arr[0]
for i in range(1, len(arr)):
current_sum = max(arr[i], current_sum + arr[i])
max_sum = max(max_sum, current_sum)
return max_sum

Time Complexity: O(n) | Space Complexity: O(1)

Q13: Two Sum Find two numbers that add up to target.

Solution (C):

int* twoSum(int arr[], int n, int target, int* returnSize) {
*returnSize = 2;
int* result = (int*)malloc(2 * sizeof(int));
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
result[0] = i;
result[1] = j;
return result;
}
}
}
return NULL;
}

Solution (Java):

public int[] twoSum(int[] arr, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
int complement = target - arr[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(arr[i], i);
}
return new int[]{};
}

Solution (Python):

def two_sum(arr, target):
seen = {}
for i, num in enumerate(arr):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []

Time Complexity: O(n) | Space Complexity: O(n)

Q14: Rotate Array Rotate array to right by k positions.

Solution (C):

void rotate(int arr[], int n, int k) {
k = k % n;
// Reverse entire array
reverse(arr, 0, n - 1);
// Reverse first k elements
reverse(arr, 0, k - 1);
// Reverse remaining elements
reverse(arr, k, n - 1);
}
void reverse(int arr[], int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

Solution (Java):

public void rotate(int[] arr, int k) {
k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

Solution (Python):

def rotate(arr, k):
k = k % len(arr)
arr.reverse()
arr[:k] = reversed(arr[:k])
arr[k:] = reversed(arr[k:])

Time Complexity: O(n) | Space Complexity: O(1)

Q15: Print Pyramid Pattern

Problem: Print pyramid pattern with n rows.

Solution (C):

void printPyramid(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++)
printf(" ");
// Print stars
for (int j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
}

Solution (Java):

public void printPyramid(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
// Print stars
for (int j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.println();
}
}

Solution (Python):

def print_pyramid(n):
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
Q16: Sum of Digits

Problem: Find sum of digits of a number.

Solution (C):

int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

Solution (Java):

public int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

Solution (Python):

def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
Q17: Armstrong Number

Problem: Check if a number is Armstrong number.

Solution (C):

int isArmstrong(int n) {
int original = n, sum = 0, digits = 0;
// Count digits
int temp = n;
while (temp > 0) {
digits++;
temp /= 10;
}
// Calculate sum
while (n > 0) {
int digit = n % 10;
sum += pow(digit, digits);
n /= 10;
}
return sum == original;
}

Solution (Java):

public boolean isArmstrong(int n) {
int original = n, sum = 0;
int digits = String.valueOf(n).length();
while (n > 0) {
int digit = n % 10;
sum += Math.pow(digit, digits);
n /= 10;
}
return sum == original;
}

Solution (Python):

def is_armstrong(n):
digits = len(str(n))
return sum(int(d)**digits for d in str(n)) == n

These problems are typically asked as the first coding problem in Infosys IRT:

  1. Prime number check
  2. Factorial calculation
  3. Fibonacci series
  4. Palindrome check
  5. Reverse string/array
  6. Sum of digits
  7. Count vowels/consonants
  8. Find maximum/minimum in array
  9. Linear search
  10. Binary search (sorted array)

These problems are typically asked as the second coding problem:

  1. Second largest element
  2. Remove duplicates
  3. Anagram check
  4. Two sum problem
  5. Maximum subarray sum (Kadane’s)
  6. Rotate array
  7. Merge sorted arrays
  8. Longest palindromic substring
  9. Valid parentheses
  10. String compression

Advanced problems (less common but good practice):

  1. Longest common subsequence
  2. Edit distance
  3. Number of islands (2D grid)
  4. Graph traversal problems
  5. Dynamic programming problems
  6. Tree traversal problems
  • Master basic array and string operations
  • Practice 5-10 easy problems daily
  • Focus on one programming language
  • Solve medium-level problems
  • Practice time-bound coding (30-35 min per problem)
  • Review previous year Infosys coding questions
  • Full-length coding practice
  • Mock tests with 2 problems in 70 minutes
  • Focus on accuracy and edge cases

Time Management

Mistake: Spending too much time on one problem
Solution: Allocate 30-35 minutes per problem, move on if stuck

Edge Cases

Mistake: Not handling edge cases (empty array, single element)
Solution: Always test with edge cases before submitting

Language Choice

Mistake: Switching languages during exam
Solution: Master one language thoroughly before exam

Code Readability

Mistake: Writing messy, unreadable code
Solution: Write clean, well-commented code for better evaluation

  1. Master One Language: Be proficient in at least one language (C/C++/Java/Python)
  2. Practice Daily: Solve 3-5 coding problems daily
  3. Time-Bound Practice: Always practice within 30-35 minutes per problem
  4. Understand Patterns: Learn common patterns (two-pointer, sliding window, etc.)
  5. Test Thoroughly: Test with edge cases before submitting
  6. Stay Calm: Don’t panic if you can’t solve immediately, think step by step

Ready to master Infosys coding? Practice these 100+ problems regularly, focus on time management, and ensure you can solve at least 1 problem correctly within 70 minutes. Download Infosys placement papers for more practice!

Pro Tip: Solve at least 50-70 coding problems before your IRT. Focus on arrays, strings, and basic algorithms as they are most frequently asked.