Time management
Mistake: Spending too much time on one problem
Solution: Allocate 30-35 minutes per problem, move on if stuck
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.
Candidates searching for Infosys previous year coding / PYQ coding questions should treat the IRT coding section as two problems: one Easy-Medium (arrays/strings) and one Medium (hashing, two pointers, basic DP/math). The set below is organized like a previous-year coding paper - practice under a 60-70 minute timer.
| PYQ coding pattern | Frequency (from student reports) | Example on this page |
|---|---|---|
| Arrays / second largest / reverse | Very high | Q1, Q2 |
| Strings / palindrome / duplicates | Very high | Q3, Q4 |
| Kadane / subarray sums | High | Q5 |
| Hashing / two sum | High | Q6 |
| Searching / merging | Medium | Q7, Q8 |
| Recursion / series | Medium | Q9, Q10 |
Given an array and target K, count unordered pairs (i, j) with i < j and arr[i] + arr[j] = K.
Approach: Hash frequency map; for each value x, add freq[K-x] (careful when 2x = K). Time O(n).
Why it shows up: Classic Infosys IRT medium coding PYQ pattern - hashing + edge cases on duplicates.
For each element, print the next greater element to its right (−1 if none).
Approach: Monotonic decreasing stack from right to left. Time O(n).
Tip: Infosys often accepts a clear O(n) stack solution over a slow nested loop that TLE on hidden tests.
| Parameter | Details |
|---|---|
| Total Problems | 2 problems |
| Time Allocated | 60-70 minutes |
| Marks | 20 marks (10 marks per problem) |
| Difficulty | Easy-Medium and Medium-Hard |
| Languages Allowed | C, C++, Java, Python |
| Platform | Online 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 secondTime 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 -= 1Time 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 TrueTime 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 + 1Time 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 TrueTime 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, cTime 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_sumTime 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)
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))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))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)) == nThese problems are typically asked as the first coding problem in Infosys IRT:
These problems are typically asked as the second coding problem:
Advanced problems (less common but good practice):
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