← Infosys Interview Insights

Infosys·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Infosys Software Engineer interview with four algorithm problems, ranging from greedy pairing to DP with constraints. The problems were more involved than I expected for this company, especially the climbing stairs one with the jump restriction.

Questions Asked (4)

Q1

You're given a list of weights and a binary category label for each element. Pair every element such that each pair has one from each category, and the cost of a pair is the max of the two weights. Minimize total cost, or return -1 if valid pairing is impossible.

Algorithms & Data Structures
Author's notes

Sorting by weight and pairing greedily felt right almost immediately, but I second-guessed myself for a bit on whether to sort ascending or descending.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, check if the counts of the two categories are equal; if not, return -1. Then sort both lists and use a greedy strategy: pair the largest weight from one category with the largest from the other, and the smallest with the smallest, to minimize the sum of maximums. Alternatively, prove that sorting and pairing in the same order (or opposite order) yields the optimal total cost.

Pro tip: Mention that the greedy approach works because the cost function is the maximum of two weights, and by sorting and pairing corresponding elements, you avoid unnecessarily large maximums. Also, clarify that if the counts are unequal, no valid pairing exists, so return -1 immediately.

1. Check feasibility

Count the number of elements in each category. If the counts are not equal, return -1 because a perfect matching is impossible.

2. Sort both lists

Sort the weights of category A and category B in ascending order. This allows for a systematic pairing strategy.

3. Pair elements

Pair the i-th smallest element of A with the i-th smallest element of B (or i-th largest with i-th largest). Compute the cost of each pair as the maximum of the two weights.

4. Sum costs

Sum the maximums of all pairs to get the total cost. This is the minimized total cost.

5. Explain optimality

Briefly justify why this pairing minimizes the sum: any other pairing would force at least one pair to have a larger maximum, increasing the total cost.

Key Points to Mention

  • Feasibility condition: equal number of elements in each category.
  • Sorting both lists to enable greedy pairing.
  • Pairing corresponding elements (smallest with smallest or largest with largest).
  • Cost of a pair is the maximum of the two weights.
  • Summing the maximums gives the total cost.
  • Proof of optimality: exchange argument or rearrangement inequality.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Partition an array into exactly K contiguous subarrays. Each subarray's value is its alternating sum. Count the number of ways to do this (or meet a target value, the exact condition was a bit unclear).

Algorithms & Data Structures
Author's notes

This one tripped me up because the problem statement felt ambiguous in the moment.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem statement, especially the exact condition (count all ways or meet a target). Then propose a dynamic programming solution that tracks the number of ways to partition the array into k subarrays, considering the alternating sum of each subarray. Discuss time and space complexity and potential optimizations.

Pro tip: Demonstrate strong communication skills by restating the problem in your own words and confirming assumptions with the interviewer before diving into the solution. This shows you value clarity and collaboration.

1. Clarify the problem

Ask questions to resolve ambiguities: Is the goal to count all partitions or those with a specific target alternating sum? What is the definition of alternating sum for a subarray? Are negative numbers allowed?

2. Define the state

Define DP[i][j] as the number of ways to partition the first i elements into j subarrays. Also track the alternating sum of the last subarray or the total alternating sum if needed.

3. Formulate transitions

For each possible last subarray ending at i, compute its alternating sum and update DP[i][j] based on DP[p][j-1] for p < i, ensuring the alternating sum condition is met.

4. Optimize if possible

Consider prefix sums or other techniques to reduce time complexity from O(K*N^2) to O(K*N) if the alternating sum can be computed efficiently.

5. Analyze complexity and edge cases

Discuss time and space complexity, and handle edge cases like K > N, empty array, or when no valid partition exists.

Key Points to Mention

  • Dynamic programming approach with state definition
  • Alternating sum calculation for subarrays
  • Time and space complexity analysis
  • Handling of edge cases (e.g., K > N, negative numbers)
  • Potential optimizations using prefix sums
  • Clarification of problem statement before solving

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

There are N stairs. You can jump 1, 2, or 3 steps at a time with costs A, B, C respectively. After a 2-step or 3-step jump, you cannot immediately take another multi-step jump. Find the minimum cost to reach the top.

Algorithms & Data Structures
Author's notes

The constraint is what makes this non-trivial.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a dynamic programming problem where the state includes the current stair and whether the previous jump was a multi-step jump. Define dp[i][0] as the minimum cost to reach stair i with no restriction on the next jump, and dp[i][1] as the minimum cost to reach stair i when the next jump cannot be multi-step. Compute transitions from smaller stairs, considering the restriction, and return the minimum cost to reach the top.

Pro tip: Clarify the cost model upfront: whether costs are per jump or per step, and whether the top is exactly stair N or beyond. This avoids off-by-one errors and ensures your solution matches the interviewer's expectations.

1. Clarify the problem

Ask questions to confirm the cost structure (e.g., cost per jump or per step), the definition of 'top' (exactly stair N or beyond), and whether the restriction applies after any multi-step jump or only after consecutive multi-step jumps.

2. Define the DP state

Define dp[i][0] as the minimum cost to reach stair i with no restriction on the next jump, and dp[i][1] as the minimum cost to reach stair i when the next jump cannot be multi-step (i.e., the previous jump was multi-step).

3. Establish base cases and transitions

Set dp[0][0] = 0 and dp[0][1] = infinity. For each stair i from 1 to N, compute dp[i][0] by taking the minimum of: dp[i-1][0] + A, dp[i-2][0] + B, dp[i-3][0] + C, dp[i-2][1] + B, dp[i-3][1] + C. Compute dp[i][1] as dp[i-1][0] + A (only a 1-step jump is allowed after a multi-step jump).

4. Compute and return the result

Iterate through stairs 1 to N, filling the DP table. The minimum cost to reach the top is min(dp[N][0], dp[N][1]). If N is 0, return 0.

5. Analyze complexity and optimize

The DP uses O(N) time and O(N) space, which can be optimized to O(1) space by keeping only the last three values of each state. Mention this optimization if asked about space complexity.

Key Points to Mention

  • Dynamic programming with state representing the current stair and whether the next jump is restricted.
  • Two states: unrestricted (0) and restricted (1) to handle the no-consecutive-multi-step-jump rule.
  • Transitions: from unrestricted, you can take any jump; from restricted, only a 1-step jump is allowed.
  • Base case: dp[0][0] = 0, dp[0][1] = infinity (or a large number).
  • Time complexity O(N) and space complexity O(N), with possible O(1) space optimization.
  • Edge cases: N=0, N=1, and large N; also clarify cost model and definition of 'top'.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

Given suppliers, hubs, and customer demand points, route goods through the hubs to satisfy all demand while minimizing total transportation cost under capacity constraints.

Algorithms & Data StructuresSystem Design
Author's notes

I remember this one being vague on the exact constraints, so I wasn't sure if a greedy with a priority queue would cut it or if they wanted something closer to min-cost flow.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a minimum-cost flow network with suppliers, hubs, and demand points as nodes, and transportation links as edges with costs and capacities. Then solve it using an algorithm like successive shortest augmenting path or linear programming, and discuss scalability and practical implementation.

Pro tip: Mention that in real-world systems, you'd likely use a solver library (e.g., OR-Tools) rather than implementing from scratch, and highlight the importance of validating the model with small test cases before scaling.

1. Clarify requirements and constraints

Ask about the scale of the problem, whether costs are linear, if there are multiple commodities, and if capacities apply to hubs or links. Confirm that all demand must be met exactly.

2. Model as a graph problem

Represent suppliers, hubs, and demand points as nodes, and transportation routes as directed edges with associated costs and capacities. Introduce a super source and super sink to handle multiple suppliers and demands.

3. Select an algorithm

For small to medium instances, use min-cost max-flow algorithms (e.g., successive shortest path with potentials). For large-scale, consider linear programming or specialized solvers.

4. Discuss implementation and complexity

Explain how to implement the chosen algorithm, including data structures (e.g., adjacency lists, priority queues) and analyze time complexity. Mention potential optimizations like capacity scaling.

5. Address scalability and real-world considerations

Talk about handling large datasets, using approximation algorithms if exact solutions are too slow, and integrating with existing systems. Mention validation and testing strategies.

Key Points to Mention

  • Minimum-cost flow formulation with super source/sink
  • Successive shortest augmenting path algorithm with potentials (Dijkstra)
  • Linear programming relaxation and duality
  • Capacity constraints on hubs and links
  • Time complexity and scalability trade-offs
  • Use of solver libraries (e.g., Google OR-Tools) in production

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.