Count Triplets Within a Value Range
Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
IBM reported this one in September 2026, and the first thing you'll notice is the input size: up to 200000 elements. That kills the triple loop before you type it. The task is counting index triplets where max minus min is at most d. It's a sort plus two pointers problem wearing a combinatorics hat. If you've got an IBM OA coming, expect to spot the pattern fast or lose time. StealthCoder sits invisibly as a safety net if you blank mid-assessment, but the idea here is short enough to own before you start.
The problem
Given an integer array values and a nonnegative integer d, count index triplets i < j < k for which the difference between the maximum and minimum selected values is at most d. Return the number of qualifying index triplets. Equal values at different indices are distinct choices. Function countBoundedTriplets(values: int[], d: int) → long Examples Example 1 values = [1,2,3,4] d = 2 return = 2 The qualifying value groups use indices for [1,2,3] and [2,3,4]. Example 2 values = [1,1,1,1] d = 0 return = 4 All four choices of three indices have range zero. Example 3 values = [1,5,9] d = 3 return = 0 The only triplet has range 8, which exceeds d. Constraints 3 <= values.length <= 200000. -10^9 <= values[i] <= 10^9. 0 <= d <= 2 * 10^9. The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Order doesn't matter for the range, only which indices you pick, so sort the array. Once sorted, any chosen triplet has its min at the leftmost pick and its max at the rightmost. For each right index k, find the smallest left index l where values[k] - values[l] <= d, using a moving pointer. Every window of size m = k - l + 1 holds C(m-1, 2) triplets that end at k, since you pick two more from the m-1 elements before k in the window. Sum those up. The pitfalls: using int for the sum when it needs 64-bit, computing the difference in 32-bit when d reaches 2 * 10^9 and values span negatives, and forgetting duplicates count as distinct. Total cost is O(n log n). If you freeze on the counting formula during the live OA, StealthCoder is the hedge that can surface it.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Count Triplets Within a Value Range 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass IBM's OA.
IBM reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Triplets Within a Value Range FAQ
What's the trick for the IBM Count Triplets Within a Value Range problem?+
Sort the array, then use two pointers. For each right end k, move the left pointer until the range is within d. Then add (window size - 1 choose 2) to the total. Sorting is legal because only the chosen indices matter, not their order.
Why can't I just use three nested loops?+
With up to 200000 elements, n cubed is far beyond anything that finishes. Even an O(n^2) approach is too slow. You need O(n log n), which means sorting plus a linear two-pointer sweep.
Which overflow traps should I watch for?+
The answer can be huge, so use a 64-bit integer for the count and for the combination math. Also compute value differences in 64-bit. Values range from -10^9 to 10^9 and d goes up to 2 * 10^9, so 32-bit arithmetic can break.
Do equal values count as separate triplets?+
Yes. The problem says equal values at different indices are distinct choices. Example 2 shows it: four ones with d = 0 gives 4 triplets, which is C(4,3). Sorting keeps duplicates in place, so the window formula handles them automatically.
How do I prepare for this in 48 hours?+
Write the sorted two-pointer solution from scratch twice. Test it on the three examples, plus an all-equal array and a window smaller than three. Practice the C(m,2) counting step until it's automatic. That covers nearly everything this problem can throw at you.