FastPrepCount Triplets With Sum at Most Target
Problems/Count Triplets With Sum at Most Target

Count Triplets With Sum at Most Target

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the goal of counting triplets and the two-pointer idea. It does not say how duplicates count, the input limits, or the function signature. Our version is about 92% the same as the reported question.

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 = 8return = 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 = 2return = 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 = 3return = 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.

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Increasing-Value Triplets Under a ThresholdIBM · OAIBM interview questions
  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • 4SumAmazon · ONSITE INTERVIEW
  • Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
  • 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
  • Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Next and Previous Lexicographic PermutationAdobe · ONSITE INTERVIEWAdobe interview questions

Interview reports that mention this question

  • Google SWE Intern Interviews: 85 Experiences (Part 1 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long countTripletsAtMost(int[] nums, long target) {
      // Write your code here.
    }
}
Checking account…