Reported September 2026
Infosyssliding window

Count Equal Code Pairs Within Distance K

Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Infosys OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Infosys OA, reported in September 2026, is the double loop. It passes the examples, then dies on 10^5 elements. The task is simple: count pairs (i, j) with i < j, equal values, and j - i <= k. The fix is a sliding window with a hash map of counts. If you've got an invite for the next day or two, learn the window shape now. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is very doable on your own.

The problem

You are given an integer array code and an integer k.
Return the number of unordered pairs (i, j) such that i < j, code[i] == code[j], and j - i <= k.

Function
countEqualCodePairs(code: int[], k: int) → long

Examples
Example 1
code = [1,2,1,1]
k = 2
return = 2
The pairs are indices (0, 2) and (2, 3). Indices (0, 3) have equal values but distance 3, which is greater than k.
Example 2
code = [1,1,1]
k = 1
return = 2
Adjacent equal pairs at distances 1 count. The pair of the first and last values has distance 2.

Constraints
0 <= code.length <= 10^5.
0 <= k <= code.length.
-10^9 <= code[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a sliding window of the last k indices plus a hash map of value counts. Walk j from left to right. Before processing j, if j - k - 1 >= 0, decrement the count of code[j-k-1], since that index just fell out of range. Then add map[code[j]] to the answer, because every equal value still in the window forms a valid pair with j. Finally increment map[code[j]]. That's O(n) time. The common pitfalls: an off-by-one on the eviction index (distance k is allowed, so the window holds k previous elements), and using a 32-bit int for the answer. With 10^5 equal values and a large k, the count hits about 5 billion, so use a long. Also handle the empty array and k = 0, which should return 0. If the window logic slips under pressure, StealthCoder can hand you the clean version live.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count Equal Code Pairs Within Distance K cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Infosys's OA.

Infosys reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Equal Code Pairs Within Distance K FAQ

What's the trick for the Infosys count equal code pairs problem?+

Keep a hash map of counts for the last k indices. For each j, add the count of code[j] in the window to the answer, then insert code[j]. Evict the element at j-k-1 as you move. It runs in O(n) instead of O(n*k).

Why does the brute force fail?+

Checking every pair is O(n^2), and checking only the next k elements is O(n*k). With length up to 10^5 and k up to the length, both can reach billions of operations. The window with a hash map removes the inner loop entirely.

Do I need a long for the answer?+

Yes. The signature returns a long for a reason. If all 10^5 values are equal and k is large, pairs reach roughly 5 * 10^9, which overflows a 32-bit int. Declare the counter as long from the start.

What edge cases should I test?+

Test an empty array, k = 0 (the answer is always 0 since j > i means distance at least 1), all equal values, and negative numbers. Also check Example 1, where indices 0 and 3 are equal but too far apart.

How do I prepare for this in 48 hours?+

Practice the fixed-size window with a frequency map until the eviction index feels automatic. Write this one from scratch twice, then test it on the two given examples. Focus on the off-by-one at j-k-1, since that's where most first attempts break.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Infosys.

OA at Infosys?
Invisible during screen share
Get it