Increasing-Value Triplets Under a Threshold
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 August 2026, and the input size is the whole story. With d.length up to 10^4, the obvious triple loop is about 10^12 operations, so it dies on the first big test. This is a sort plus two-pointers problem dressed up as a triplet count. Since positions don't matter, you're really counting 3-element subsets of distinct values with sum <= t. If you freeze when you see the threshold and the 64-bit warning, StealthCoder is the safety net that runs invisibly during the live OA. The trick itself is short.
The problem
Given an array of distinct integers d and a threshold t, return the number of triplets of distinct indices (a, b, c) that satisfy both conditions: d[a] < d[b] < d[c]. d[a] + d[b] + d[c] <= t. The indices identify three distinct elements. Their original positions do not need to satisfy a < b < c; the tuple is ordered by the selected values. Use 64-bit arithmetic for t, each sum, and the returned count. Implement triplets(long t, int[] d). Function triplets(t: long, d: int[]) → long Examples Example 1 t = 8 d = [1, 2, 3, 4, 5] return = 4 The four valid value triplets are: (1, 2, 3), whose sum is 6. (1, 2, 4), whose sum is 7. (1, 2, 5), whose sum is 8. (1, 3, 4), whose sum is 8. Example 2 t = 7 d = [4, 1, 2] return = 1 The selected values (1, 2, 4) have sum 7. Their original indices are (1, 2, 0), so they count even though those positions are not increasing. Example 3 t = 2500000000 d = [800000000, 800000001, 800000002] return = 1 The only possible triplet sums to 2400000003, which does not exceed 2500000000. The threshold is greater than 2^31 - 1, so it requires a 64-bit type. Constraints 1 <= d.length <= 10^4. All values in d are distinct. 0 < d[i] < 10^9. 0 < t < 3 * 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort the array. The ordering condition d[a] < d[b] < d[c] with distinct values just means you pick any 3 elements, so original indices are irrelevant. Fix the smallest element at index i, then run two pointers l = i+1 and r = n-1. If d[i]+d[l]+d[r] <= t, every index from l+1 to r pairs with l as the third element, so add r - l and move l up. Otherwise move r down. That's O(n^2), about 10^8 steps at worst, which is fine. The pitfalls: summing in int overflows since three values near 10^9 exceed 2^31, and the count can reach roughly 1.6 x 10^11, so use long for both. Don't dedupe, values are already distinct. If you blank on the counting step, StealthCoder is your hedge during the live OA, but the two-pointer idea is only a few lines once you see it.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Increasing-Value Triplets Under a Threshold 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as 3sum smaller. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass IBM's OA.
IBM reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Increasing-Value Triplets Under a Threshold FAQ
What's the trick for the IBM increasing-value triplets problem?+
Sort first, since index order doesn't matter. Fix the smallest value, then use two pointers on the rest. When the sum fits under t, all elements between the pointers work with the left one, so add r - l. That gives O(n^2) instead of O(n^3).
Why does brute force fail here?+
With n up to 10^4, three nested loops means roughly 10^12 checks for the worst case. That times out. Even n choose 3 is about 1.6 x 10^11 triplets, so you can't enumerate them one by one. You need to count in batches with two pointers.
Do I need long for everything?+
Yes for the sum, the threshold, and the result. Three values near 10^9 sum to about 3 x 10^9, which overflows a 32-bit int. The answer can also exceed 2^31, so the counter must be 64-bit. In Java, declare the accumulator as long and cast carefully.
Does the original index order matter?+
No. Example 2 shows [4, 1, 2] with t = 7 returns 1, because values (1, 2, 4) count even though their indices aren't increasing. Sorting is safe because you're counting value triples, not index triples.
How do I prepare for this in 48 hours?+
Practice the sort plus two-pointers counting pattern, like 3Sum smaller. Write it once from scratch, then test with the three examples, especially the overflow case. Focus on the r - l counting step and long types. That covers nearly everything this problem tests.