← Infosys Interview Insights

Infosys·Software Engineer·Online Assessment (OA)·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Got wrecked by an Infosys OA. Couldn't even brute force the problem, which says a lot about where my DP skills are right now.

Questions Asked (1)

Q1

Given an array of coin denominations with unlimited supply and a target amount, find the number of distinct combinations that sum to the target. An additional constraint D is introduced: a combination is only valid if any two distinct denominations used differ by at least D. Using the same denomination multiple times is fine. Return the answer modulo 1000000007.

Algorithms & Data Structures
Author's notes

Couldn't even get a brute force working, which was pretty embarrassing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a dynamic programming (DP) problem where dp[i] represents the number of valid combinations summing to amount i. Iterate over denominations in sorted order, and for each denomination, update dp from that denomination up to the target, ensuring that when adding a new denomination, the previous denomination used (if any) differs by at least D. Finally, return dp[target] modulo 1000000007.

Pro tip: Clarify whether the constraint applies to any two distinct denominations in the combination or only to consecutive denominations in sorted order; the latter is more common and simplifies the DP. Also, mention that sorting the denominations and using a 2D DP state (amount, last denomination index) elegantly handles the constraint.

1. Understand the problem and constraints

Restate the problem: count distinct combinations of unlimited coins summing to target, with the extra rule that any two distinct denominations used must differ by at least D. Clarify edge cases: D=0 means no restriction, D larger than max difference may yield zero combinations.

2. Sort denominations and define DP state

Sort the denominations ascending. Define dp[i][j] as the number of valid combinations summing to amount i where the last (largest) denomination used has index j. This captures the constraint because any new denomination k must satisfy denom[k] - denom[j] >= D.

3. Initialize and transition

Initialize dp[0][j] = 1 for all j (empty combination) or handle base case separately. For each amount i from 1 to target, and for each denomination j, if i >= denom[j], dp[i][j] = sum over k <= j with denom[j] - denom[k] >= D of dp[i - denom[j]][k] plus dp[i - denom[j]][j] (using same denomination again).

4. Optimize and compute final answer

Optimize the transition using prefix sums or by iterating denominations in order and maintaining cumulative sums. The final answer is sum over all j of dp[target][j] modulo 1000000007.

5. Analyze complexity and test

Time complexity O(target * n) with optimization, space O(target * n) or O(target) with careful iteration. Test with small examples, including D=0 and cases where no combination exists.

Key Points to Mention

  • Dynamic programming with state (amount, last denomination index) to enforce the difference constraint.
  • Sorting denominations to ensure the constraint is checked in order.
  • Modulo operation at each addition to prevent overflow.
  • Handling unlimited supply by allowing reuse of the same denomination (transition from same j).
  • Edge cases: D=0, target=0, no valid combinations.
  • Time and space complexity analysis and possible optimizations.

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