Pair sums need complement counts; longest-unique substrings need a moving boundary; repeated range sums need prefixes; and unique three-sum output needs sorting plus two pointers. Infer the invariant from the input-output contract, trace each state change, then check complexity. Follow your Infosys assessment communication for live format details. For wider context, read the Infosys Placement Preparation Guide.
Infosys coding questions: identify the pattern before writing code
question signal | pattern | invariant | target complexity |
|---|---|---|---|
count index pairs whose values sum to K in an unsorted array | frequency map |
|
|
longest contiguous substring with no repeated character | sliding window | the current window contains unique characters |
|
many range-sum queries on one static array | prefix sum |
|
|
unique triplets with a target sum | sort plus two pointers | moving left raises the sum and moving right lowers it |
|
Signals suggest a method; constraints test it. Place coding practice beside aptitude and interview preparation with Company-Specific Placement Courses.
Patterns 1 and 2: preserve valid state during one scan
Pattern 1, frequency map: count duplicate pair sums without double counting
Consider this illustrative practice problem: A = [1, 5, 7, -1, 5] and K = 6. Count index pairs (i,j) for which i < j and A[i] + A[j] = 6. Equal values at different indices are different choices, so the expected answer is 3: (0,1), (0,4), and (2,3).
The invariant is that seen[v] counts occurrences strictly before the current index. For each value x, add seen[K - x] before incrementing seen[x]. This prevents self-pairing.
The scan is exact. For 1, complement 5 adds 0, so total 0. The first 5 needs 1 and adds 1, so total 1. Then 7 needs -1 and adds 0. Next, -1 needs 7 and adds 1, so total 2. The second 5 needs 1 and adds 1, giving 3. Increment seen[x] after counting on every row.
A set detects a complement but loses multiplicity. The frequency map takes O(n) expected time and O(n) space without double counting.
![Scan table tracing the frequency-map count of index pairs summing to 6 in [1, 5, 7, -1, 5], reaching a total of 3.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784675387286_iyf054.jpg)
Pattern 2, sliding window: find the longest substring with unique characters
Take s = "abcaefb". Maintain the valid window's first index, left, and a lastSeen map. At zero-based right indices 0, 1, and 2, the windows are "a", "ab", and "abc", so the best length reaches 3.
At right index 3, a was last seen at 0. Set left = max(0, 0 + 1) = 1, which makes the valid window "bca". Adding e gives "bcae", length 4, and adding f gives "bcaef", length 5. At the final b, its previous index is 1, so set left = max(1, 1 + 1) = 2. The window becomes "caefb", also length 5.
The output is 5, with two maximal windows: "bcaef" and "caefb". The active window contains unique characters, and every index enters and leaves it at most once. This gives O(n) time and up to O(k) character space. Never use left = lastSeen[ch] + 1 without max; on "abba", it can move the boundary backwards.
Patterns 3 and 4: preprocess once, then answer efficiently
Pattern 3, prefix sum: answer repeated range queries without rescanning
For the static array A = [3, -2, 5, 1, -4, 2], define P[0] = 0 and P[i+1] = P[i] + A[i]. Building from left to right produces P = [0, 3, 1, 6, 7, 3, 5].
Now answer zero-based inclusive queries. For [1,3], calculate P[4] - P[1] = 7 - 3 = 4, which matches -2 + 5 + 1 = 4. For [2,5], calculate P[6] - P[2] = 5 - 1 = 4, matching 5 + 1 - 4 + 2 = 4. The output list is [4,4].
P[i] stores the sum strictly before index i, so P[r+1] - P[l] removes everything before l but keeps A[r]. Rescanning for q queries costs O(nq) in the worst case. Preprocessing costs O(n + q) total, O(n) extra space, and O(1) per query. Updates need a different data structure.
![Prefix-sum strip for [3, -2, 5, 1, -4, 2] showing P = [0, 3, 1, 6, 7, 3, 5] and two range queries that each total 4.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784675388395_7ckbem.jpg)
Pattern 4, sorting and two pointers: return unique three-sum combinations
Use A = [0, -1, 2, -4, 1, -1] with target 0. Sorting gives [-4, -1, -1, 0, 1, 2]. At each fixed index i, put left = i + 1 and right = n - 1. Move left for a small sum and right for a large sum.
With fixed -4, the first sum is -4 + -1 + 2 = -3. Advancing left gives another -3, then -2, then -1; all are too small, so nothing is recorded. With the first fixed -1 at index 1, -1 + -1 + 2 = 0, so record [-1,-1,2] and move both pointers. Now -1 + 0 + 1 = 0, so record [-1,0,1]. Skip the second fixed -1 because it duplicates the previous fixed value. No later fixed value produces another zero sum.
The exact output is [[-1,-1,2],[-1,0,1]]. Skip repeated fixed values and repeated pointer values after a hit. Sorting costs O(n log n) and scanning costs O(n^2), so the total is O(n^2). The scan uses constant extra space apart from output, though sorting may need stack or buffer space.
Test each coding invariant with adversarial micro-tests
Contract mistakes can survive ordinary examples. Pair each failure with a repair:
Treating value pairs as index pairs loses duplicate combinations, so define the pair contract before coding.
Inserting
xbefore checking its complement can pair an element with itself, so query before incrementing.Using
P[r] - P[l]drops the right endpoint, so useP[r+1] - P[l].Omitting duplicate skips in three-sum repeats output triplets, so skip equal fixed and pointer values.
Use focused micro-tests. Pair count on A = [3,3,3], K = 6 must return 3 index pairs. Longest-unique on "abba" returns 2. Prefix array [5] with query [0,0] returns 5. Three-sum on [0,0,0,0] returns one unique triplet, [[0,0,0]]. Empty pair and substring inputs return 0.
Reading constraints, dry-running, and protecting review time are central habits in Coding Round Strategy for Placements.
Infosys coding practice: one invariant, three retrieval drills
Use a three-pass loop to make each method retrievable: reproduce the dry run by hand, implement from a blank editor with the invariant as a comment, then mutate one input and predict the output before running it.
Change the pair target from 6 to 10 and verify that the two 5 values form one index pair. Append c to "abcaefb" and recompute the window rather than guessing. Add query [0,5] to the prefix example and obtain total 5. Change the three-sum target to 1 and verify that the only unique triplet is [-1,0,2].
If assignment order, loop boundaries, or nested conditions still cause errors, practise Infosys Pseudocode Questions before returning to implementation.
Infosys coding questions: the short version and next step
Keep four cues ready: complement counts produce 3; the unique-character window reaches 5; both range queries return 4; and duplicate removal leaves two unique zero-sum triplets. Explain each invariant before revisiting syntax.
Stable methods matter more than memorising the format of a current Infosys drive. Follow your assessment communication for live specifics. For wider preparation, use the Infosys Superset Preparation Course.




