Reported September 2026
Googletwo pointers

Count Triplets With Sum at Most Target

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

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

Google reported this one in September 2026, and the constraint is the whole story. With nums.length up to 2000, a triple loop is around 1.3 billion combinations, and that's a fast way to time out. The OA wants you to see sorting plus two pointers, which drops it to O(n^2). Count triplets with sum at most target, index combinations only, so duplicate values still count separately. If you blank under the clock, StealthCoder is the invisible safety net that reads the problem and hands you the approach live. But the pattern is short enough to own tonight.

The problem

Given an integer array nums and an integer target, return the number of index triplets (i, j, k) such that:
0 <= i < j < k < nums.length.
nums[i] + nums[j] + nums[k] <= target.
Each distinct index combination counts separately, even when multiple indices contain the same value.
Use 64-bit arithmetic for every three-value sum and for the returned count.

Function
countTripletsAtMost(nums: int[], target: long) → long

Examples
Example 1
nums = [1,2,3,4,5]
target = 8
return = 4
The valid index triplets select values (1,2,3), (1,2,4), (1,2,5), and (1,3,4). Their sums are 6, 7, 8, and 8.
Example 2
nums = [-2,0,1,3]
target = 2
return = 3
Three triplets have sums at most 2: (-2,0,1), (-2,0,3), and (-2,1,3). The remaining triplet sums to 4.
Example 3
nums = [1,1,1,1]
target = 3
return = 4
Every choice of three distinct indices has sum 3. There are 4 such index combinations.

Constraints
3 <= nums.length <= 2000.
-10^9 <= nums[i] <= 10^9.
-10^9 <= target <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort the array first. Order of indices doesn't matter for the count, since you're counting unordered index sets. Fix i as the smallest element, then set left = i+1 and right = n-1. If nums[i] + nums[left] + nums[right] <= target, then every index between left and right pairs validly with that left, so add right - left to the count and move left up. Otherwise move right down. That's O(n^2) total with O(1) extra space. The pitfalls are real. Sums can hit 3 billion, so use 64-bit for the sum and the count, or you'll overflow a 32-bit int. Don't dedupe equal values, because the problem says each index combination counts. Negative targets and values work fine with this approach. If you freeze during the live OA, StealthCoder can be your hedge, but trace Example 1 by hand once and you're set.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Count Triplets With Sum at Most Target 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as 3sum smaller. If you have time before the OA, drill that.

⏵ The honest play

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

Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Triplets With Sum at Most Target FAQ

What's the trick to Count Triplets With Sum at Most Target?+

Sort, then fix one index and run two pointers on the rest. When the sum at left and right fits under target, all elements from left+1 through right also work with left, so you add right - left at once. That collapses the inner loop into a linear scan.

Why does brute force fail here?+

Length goes up to 2000, so three nested loops mean roughly 1.3 billion triplet checks. That's too slow for a typical OA. The O(n^2) two-pointer approach does about 2 million pointer steps at most, which is trivial.

Do I skip duplicate values?+

No. The problem says each distinct index combination counts separately even if values match. Example 3 with [1,1,1,1] returns 4, not 1. Skipping duplicates like in 3Sum would give the wrong answer, so don't copy that habit.

Where do overflow bugs come from?+

Three values near 10^9 sum to 3 billion, which exceeds a 32-bit int. Cast to long before adding. The count can also be large, up to about 1.3 billion, so return a long. Languages like Python don't care, but Java and C++ will punish you.

How do I prepare for this in 48 hours?+

Write the sorted two-pointer solution from scratch twice. Then trace the three examples by hand, especially the negative one. Also review 3Sum and 3Sum Smaller, since this is a close cousin. Focus on why adding right - left is correct, because that's the step people forget.

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

OA at Google?
Invisible during screen share
Get it