Top 10 Infosys Coding Interview Questions from 2025
Introduction
In this blog post, we'll share the most commonly asked coding interview questions at Infosys. If you don't have months to study for your interviews, you can use AI tools like Chatmagic to generate solutions quickly and efficiently - helping you pass the interviews and get the job offer!
Problem #1: Maximize the Minimum Game Score
You are given an array points of size n and an integer m. There is another array gameScore of size n, where gameScore[i] represents the score achieved at the ith game. Initially, gameScore[i] == 0 for all i. You start at index -1, which is outside the array (before the first position at index 0). You can make at most m moves. In each move, you can either: Increase the index by 1 and add points[i] to gameScore[i]. Decrease the index by 1 and add points[i] to gameScore[i]. Note that the index must always remain within the bounds of the array after the first move. Return the maximum possible minimum value in gameScore after at most m moves. Example 1: Input: points = [2,4], m = 3 Output: 4 Explanation: Initially, index i = -1 and gameScore = [0, 0]. Move Index gameScore Increase i 0 [2, 0] Increase i 1 [2, 4] Decrease i 0 [4, 4] The minimum value in gameScore is 4, and this is the maximum possible minimum among all configurations. Hence, 4 is the output. Example 2: Input: points = [1,2,3], m = 5 Output: 2 Explanation: Initially, index i = -1 and gameScore = [0, 0, 0]. Move Index gameScore Increase i 0 [1, 0, 0] Increase i 1 [1, 2, 0] Decrease i 0 [2, 2, 0] Increase i 1 [2, 4, 0] Increase i 2 [2, 4, 3] The minimum value in gameScore is 2, and this is the maximum possible minimum among all configurations. Hence, 2 is the output. Constraints: 2 <= n == points.length <= 5 * 104 1 <= points[i] <= 106 1 <= m <= 109
Topics: Array, Binary Search, Greedy
Problem #2: Find the Number of Subsequences With Equal GCD
You are given an integer array nums. Your task is to find the number of pairs of non-empty subsequences (seq1, seq2) of nums that satisfy the following conditions: The subsequences seq1 and seq2 are disjoint, meaning no index of nums is common between them. The GCD of the elements of seq1 is equal to the GCD of the elements of seq2. Return the total number of such pairs. Since the answer may be very large, return it modulo 109 + 7. Example 1: Input: nums = [1,2,3,4] Output: 10 Explanation: The subsequence pairs which have the GCD of their elements equal to 1 are: ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) Example 2: Input: nums = [10,20,30] Output: 2 Explanation: The subsequence pairs which have the GCD of their elements equal to 10 are: ([10, 20, 30], [10, 20, 30]) ([10, 20, 30], [10, 20, 30]) Example 3: Input: nums = [1,1,1,1] Output: 50 Constraints: 1 <= nums.length <= 200 1 <= nums[i] <= 200
Topics: Array, Math, Dynamic Programming, Number Theory
Problem #3: Transform Array by Parity
You are given an integer array nums. Transform nums by performing the following operations in the exact order specified: Replace each even number with 0. Replace each odd numbers with 1. Sort the modified array in non-decreasing order. Return the resulting array after performing these operations. Example 1: Input: nums = [4,3,2,1] Output: [0,0,1,1] Explanation: Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, nums = [0, 1, 0, 1]. After sorting nums in non-descending order, nums = [0, 0, 1, 1]. Example 2: Input: nums = [1,5,1,4,2] Output: [0,0,1,1,1] Explanation: Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, nums = [1, 1, 1, 0, 0]. After sorting nums in non-descending order, nums = [0, 0, 1, 1, 1]. Constraints: 1 <= nums.length <= 100 1 <= nums[i] <= 1000
Topics: Array, Sorting, Counting
Problem #4: Eat Pizzas!
You are given an integer array pizzas of size n, where pizzas[i] represents the weight of the ith pizza. Every day, you eat exactly 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights W, X, Y, and Z, where W <= X <= Y <= Z, you gain the weight of only 1 pizza! On odd-numbered days (1-indexed), you gain a weight of Z. On even-numbered days, you gain a weight of Y. Find the maximum total weight you can gain by eating all pizzas optimally. Note: It is guaranteed that n is a multiple of 4, and each pizza can be eaten only once. Example 1: Input: pizzas = [1,2,3,4,5,6,7,8] Output: 14 Explanation: On day 1, you eat pizzas at indices [1, 2, 4, 7] = [2, 3, 5, 8]. You gain a weight of 8. On day 2, you eat pizzas at indices [0, 3, 5, 6] = [1, 4, 6, 7]. You gain a weight of 6. The total weight gained after eating all the pizzas is 8 + 6 = 14. Example 2: Input: pizzas = [2,1,1,1,1,1,1,1] Output: 3 Explanation: On day 1, you eat pizzas at indices [4, 5, 6, 0] = [1, 1, 1, 2]. You gain a weight of 2. On day 2, you eat pizzas at indices [1, 2, 3, 7] = [1, 1, 1, 1]. You gain a weight of 1. The total weight gained after eating all the pizzas is 2 + 1 = 3. Constraints: 4 <= n == pizzas.length <= 2 * 105 1 <= pizzas[i] <= 105 n is a multiple of 4.
Topics: Array, Greedy, Sorting
Problem #5: Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints: 2 <= nums.length <= 104 -109 <= nums[i] <= 109 -109 <= target <= 109 Only one valid answer exists. Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
Topics: Array, Hash Table
Problem #6: Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without duplicate characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints: 0 <= s.length <= 5 * 104 s consists of English letters, digits, symbols and spaces.
Topics: Hash Table, String, Sliding Window
Problem #7: Find Building Where Alice and Bob Can Meet
You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building. If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j]. You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi. Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1. Example 1: Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] Output: [2,5,-1,5,2] Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. In the third query, Alice cannot meet Bob since Alice cannot move to any other building. In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. In the fifth query, Alice and Bob are already in the same building. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. Example 2: Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] Output: [7,6,-1,4,6] Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. In the third query, Alice cannot meet Bob since Bob cannot move to any other building. In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. Constraints: 1 <= heights.length <= 5 104 1 <= heights[i] <= 109 1 <= queries.length <= 5 104 queries[i] = [ai, bi] 0 <= ai, bi <= heights.length - 1
Topics: Array, Binary Search, Stack, Binary Indexed Tree, Segment Tree, Heap (Priority Queue), Monotonic Stack
Problem #8: Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0. Constraints: 1 <= prices.length <= 105 0 <= prices[i] <= 104
Topics: Array, Dynamic Programming
Problem #9: Rotate Image
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Constraints: n == matrix.length == matrix[i].length 1 <= n <= 20 -1000 <= matrix[i][j] <= 1000
Topics: Array, Math, Matrix
Problem #10: Move Zeroes
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Example 2: Input: nums = [0] Output: [0] Constraints: 1 <= nums.length <= 104 -231 <= nums[i] <= 231 - 1 Follow up: Could you minimize the total number of operations done?
Topics: Array, Two Pointers