Infosys Coding Round Questions with Solutions
Last updated 2026-11-19
Infosys sets two kinds of coding tests: a basic one for System Engineer roles and a harder one for specialist tracks and the HackWithInfy contest. This page practises the harder kind, with ten problems and worked Python and Java solutions.
Read how to crack the DSA coding round for strategy, and Infosys hiring for sourced news on contests.
| Harder track | Specialist Programmer / HackWithInfy |
|---|---|
| Languages | Python and Java shown |
| Topics | Hashing, DP, grids, windows, binary search |
What the Infosys coding assessments look like
Infosys's published fresher guidance is thin, and prep sources describe two flavours. The standard System Engineer assessment is commonly reported to include pseudocode and basic coding. The specialist routes, including the HackWithInfy contest (registration dates reported by Talentd), are described as harder, with several medium to hard algorithm problems under a time limit. The problems below lean towards that harder flavour because pseudocode tracing is covered on Infosys interview questions. They are practice problems in the style of the topics, not a recorded paper.
Patterns worth knowing
- Hash map lookups for pair and prefix-sum problems.
- Sliding window and two pointers for substring and array problems.
- Dynamic programming with a clear state definition (coin change, LIS).
- Graph traversal on grids with BFS or DFS.
- Binary search, including on answers and tails arrays.
Infosys Coding Round Questions with Solutions: questions with worked answers
1 Two Sum with indices
easyStore each value's index in a dictionary. For each number, check whether target minus it was seen earlier. One pass, no sorting, so original indices are kept. Complexity: O(n) time, O(n) space. Common mistakes: reusing the same element twice (check the map before inserting) and sorting first, which loses the indices.
pythondef two_sum(a, t): seen = {} for i, x in enumerate(a): if t - x in seen: return [seen[t - x], i] seen[x] = i return [] print(two_sum([2, 7, 11, 15], 9)) # [0, 1]javaimport java.util.*; class Solution { static int[] twoSum(int[] a, int t) { Map<Integer, Integer> seen = new HashMap<>(); for (int i = 0; i < a.length; i++) { Integer j = seen.get(t - a[i]); if (j != null) return new int[]{j, i}; seen.put(a[i], i); } return new int[0]; } }2 Merge overlapping intervals
mediumSort by start. Walk through; if the next start is at most the current end, extend the end with max; otherwise start a new interval. Complexity: O(n log n) time, O(n) space. Common mistakes: forgetting to sort, and assigning the end instead of taking the max when one interval contains another.
pythondef merge(iv): iv.sort() out = [] for s, e in iv: if out and s <= out[-1][1]: out[-1][1] = max(out[-1][1], e) else: out.append([s, e]) return out print(merge([[1, 3], [2, 6], [8, 10], [15, 18]])) # [[1,6],[8,10],[15,18]]javaimport java.util.*; class Solution { static int[][] merge(int[][] iv) { Arrays.sort(iv, (x, y) -> Integer.compare(x[0], y[0])); List<int[]> out = new ArrayList<>(); for (int[] c : iv) { if (!out.isEmpty() && c[0] <= out.get(out.size() - 1)[1]) out.get(out.size() - 1)[1] = Math.max(out.get(out.size() - 1)[1], c[1]); else out.add(new int[]{c[0], c[1]}); } return out.toArray(new int[0][]); } }3 Longest substring without repeating characters
mediumSliding window with a map of last-seen index. When a character repeats inside the window, move the left edge to one past its previous position. Complexity: O(n) time, O(min(n, alphabet)) space. Common mistakes: moving the left edge backwards (use max with the current left) and resetting the whole window on a repeat.
pythondef longest_unique(s): last, left, best = {}, 0, 0 for i, c in enumerate(s): if c in last and last[c] >= left: left = last[c] + 1 last[c] = i best = max(best, i - left + 1) return best print(longest_unique("abcabcbb")) # 3javaimport java.util.*; class Solution { static int longestUnique(String s) { Map<Character, Integer> last = new HashMap<>(); int left = 0, best = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (last.containsKey(c) && last.get(c) >= left) left = last.get(c) + 1; last.put(c, i); best = Math.max(best, i - left + 1); } return best; } }4 Minimum coins to make an amount
mediumBottom-up DP: dp[a] is the fewest coins for amount a, dp[0] = 0, and dp[a] = min(dp[a - c] + 1) over coins c not exceeding a. Greedy fails for sets like {1, 3, 4} with amount 6. Complexity: O(amount * coins) time, O(amount) space. Common mistakes: using greedy, and returning the sentinel (infinity) instead of -1 when the amount is unreachable.
pythondef coin_change(coins, amount): INF = amount + 1 dp = [0] + [INF] * amount for a in range(1, amount + 1): for c in coins: if c <= a: dp[a] = min(dp[a], dp[a - c] + 1) return dp[amount] if dp[amount] != INF else -1 print(coin_change([1, 3, 4], 6), coin_change([2], 3)) # 2 -1javaimport java.util.*; class Solution { static int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, amount + 1); dp[0] = 0; for (int a = 1; a <= amount; a++) for (int c : coins) if (c <= a) dp[a] = Math.min(dp[a], dp[a - c] + 1); return dp[amount] > amount ? -1 : dp[amount]; } }5 Number of islands in a grid
mediumScan the grid; each time you find an unvisited land cell, count one island and flood-fill all connected land (4 directions) by marking it visited. Use an iterative stack to avoid recursion limits. Complexity: O(rows * cols) time, O(rows * cols) worst-case stack. Common mistakes: counting diagonal neighbours as connected and mutating the input when the caller still needs it.
pythondef islands(g): if not g: return 0 R, C, n = len(g), len(g[0]), 0 seen = set() for r in range(R): for c in range(C): if g[r][c] == '1' and (r, c) not in seen: n += 1 st = [(r, c)]; seen.add((r, c)) while st: x, y = st.pop() for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): a, b = x + dx, y + dy if 0 <= a < R and 0 <= b < C and g[a][b] == '1' and (a, b) not in seen: seen.add((a, b)); st.append((a, b)) return n print(islands(["110", "010", "001"])) # 2javaimport java.util.*; class Solution { static int islands(char[][] g) { int R = g.length, C = R == 0 ? 0 : g[0].length, n = 0; boolean[][] seen = new boolean[R][C]; int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) { if (g[r][c] != '1' || seen[r][c]) continue; n++; Deque<int[]> st = new ArrayDeque<>(); st.push(new int[]{r, c}); seen[r][c] = true; while (!st.isEmpty()) { int[] p = st.pop(); for (int[] k : d) { int a = p[0] + k[0], b = p[1] + k[1]; if (a >= 0 && a < R && b >= 0 && b < C && g[a][b] == '1' && !seen[a][b]) { seen[a][b] = true; st.push(new int[]{a, b}); } } } } return n; } }6 Product of array except self, without division
mediumBuild a prefix product pass left to right into the result, then multiply by a running suffix product right to left. No division, so zeros are handled correctly. Complexity: O(n) time, O(1) extra space besides the output. Common mistakes: dividing by the total product (fails with zeros) and using two full extra arrays when one pass variable is enough.
pythondef product_except_self(a): n = len(a); out = [1] * n p = 1 for i in range(n): out[i] = p; p *= a[i] s = 1 for i in range(n - 1, -1, -1): out[i] *= s; s *= a[i] return out print(product_except_self([1, 2, 3, 4])) # [24, 12, 8, 6]javaclass Solution { static int[] productExceptSelf(int[] a) { int n = a.length; int[] out = new int[n]; int p = 1; for (int i = 0; i < n; i++) { out[i] = p; p *= a[i]; } int s = 1; for (int i = n - 1; i >= 0; i--) { out[i] *= s; s *= a[i]; } return out; } }7 Subarray sum equals K (with negatives)
mediumPrefix sums plus a hash map of how often each prefix has occurred. At each position, the number of subarrays ending here with sum K equals count[prefix - K]. Start the map with {0: 1}. Complexity: O(n) time, O(n) space. Common mistakes: using a sliding window (breaks with negative numbers) and forgetting the initial {0: 1} so subarrays starting at index 0 are missed.
pythondef subarray_sum(a, k): cnt = {0: 1}; pre = res = 0 for x in a: pre += x res += cnt.get(pre - k, 0) cnt[pre] = cnt.get(pre, 0) + 1 return res print(subarray_sum([1, 2, 3, -3, 3], 3)) # 5javaimport java.util.*; class Solution { static int subarraySum(int[] a, int k) { Map<Integer, Integer> cnt = new HashMap<>(); cnt.put(0, 1); int pre = 0, res = 0; for (int x : a) { pre += x; res += cnt.getOrDefault(pre - k, 0); cnt.merge(pre, 1, Integer::sum); } return res; } }8 Top K frequent elements
mediumCount with a hash map, then keep a min-heap of size K keyed by frequency, or use bucket sort by frequency for linear time. The heap version is easier to write correctly under time pressure. Complexity: O(n log k) time, O(n) space. Common mistakes: sorting the whole frequency list when K is small and returning counts instead of the elements.
pythonimport heapq from collections import Counter def top_k(a, k): return [x for x, _ in heapq.nlargest(k, Counter(a).items(), key=lambda p: p[1])] print(sorted(top_k([1, 1, 1, 2, 2, 3], 2))) # [1, 2]javaimport java.util.*; class Solution { static int[] topK(int[] a, int k) { Map<Integer, Integer> cnt = new HashMap<>(); for (int x : a) cnt.merge(x, 1, Integer::sum); PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> cnt.get(x) - cnt.get(y)); for (int x : cnt.keySet()) { pq.add(x); if (pq.size() > k) pq.poll(); } int[] out = new int[pq.size()]; for (int i = out.length - 1; i >= 0; i--) out[i] = pq.poll(); return out; } }9 Longest increasing subsequence in O(n log n)
hardMaintain a list tails where tails[i] is the smallest tail value of an increasing subsequence of length i+1. For each number, binary-search the first tail that is at least x (strict increase) and replace it, or append. The list length is the answer, though the list itself is not a valid subsequence. Complexity: O(n log n) time, O(n) space. Common mistakes: using bisect_right (allows equal values, which breaks strictness) and printing the tails as if it were the sequence.
pythonfrom bisect import bisect_left def lis(a): tails = [] for x in a: i = bisect_left(tails, x) if i == len(tails): tails.append(x) else: tails[i] = x return len(tails) print(lis([10, 9, 2, 5, 3, 7, 101, 18])) # 4javaimport java.util.*; class Solution { static int lis(int[] a) { int[] tails = new int[a.length]; int size = 0; for (int x : a) { int lo = 0, hi = size; while (lo < hi) { int mid = (lo + hi) / 2; if (tails[mid] < x) lo = mid + 1; else hi = mid; } tails[lo] = x; if (lo == size) size++; } return size; } }10 Trapping rain water
hardTwo pointers from both ends with running left and right maxima. Water above a bar is limited by the smaller of the two maxima, so always advance the side with the lower maximum. Complexity: O(n) time, O(1) space. Common mistakes: using the global maximum for every bar and not updating the running maximum before computing the trapped amount.
pythondef trap(h): l, r = 0, len(h) - 1 lm = rm = water = 0 while l < r: if h[l] < h[r]: lm = max(lm, h[l]); water += lm - h[l]; l += 1 else: rm = max(rm, h[r]); water += rm - h[r]; r -= 1 return water print(trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])) # 6javaclass Solution { static int trap(int[] h) { int l = 0, r = h.length - 1, lm = 0, rm = 0, water = 0; while (l < r) { if (h[l] < h[r]) { lm = Math.max(lm, h[l]); water += lm - h[l]; l++; } else { rm = Math.max(rm, h[r]); water += rm - h[r]; r--; } } return water; } }
Frequently asked questions
What is the HackWithInfy difficulty?
Prep sources describe it as medium to hard algorithmic problems. Infosys has not published a problem set, so use practice problems of this type.
Which topics matter most for Infosys coding?
Arrays, strings, hashing, dynamic programming and graph traversal on grids appear most often in prep material.
Can I use any language?
Check the platform instructions of your test. Python, Java and C++ are widely supported in such assessments.
How do I improve speed?
Practise writing the brute force quickly, then optimise, and keep a template for input reading.
Sources
Related guides
More in Company Interview Questions
- Infosys Interview Questions for Freshers
- Intel Coding Round Questions with Solutions
- Intel Interview Questions and Answers for India Software Roles
- Intuit Coding Round Questions with Solutions in Python and Java
- Intuit Interview Questions and Answers for SDE-1 Roles
- JPMorgan Chase interview questions with answers (software engineer)
- JPMorgan coding round questions with solutions
- Juspay Coding Round Questions
- Juspay Interview Questions with Answers
- KPIT coding round questions
- KPIT interview questions and answers
- KPMG coding round questions