FastPrepCount Distinct Pairs
Problems/Count Distinct Pairs

Count Distinct Pairs

Google logoGoogle● MediumONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Backed by a real Google onsite interview report. The report does not say how to treat negative inputs or leading zeros. Here, inputs are non-negative integers, and leading zeros follow the usual integer rules. Our version is about 95% the same as the reported question.

You are given an array nums of non-negative integers. Count the index pairs (i, j) with 0 <= i < j < nums.length where nums[i] can be made from nums[j] by swapping at most one pair of decimal digit positions.

Making no swap is allowed, so equal values always form a valid pair. A swap may put one or more zeros at the front of the number. In that case, read the result as a normal integer. Leading zeros do not change its value.

Function

countDistinctPairs(nums: int[]) → long

Examples

Example 1

nums = [1, 23, 156, 4738, 321, 72992, 231, 651, 32]return = 3

The valid pairs are 23 with 32, 156 with 651, and 321 with 231. In each pair, swapping two digit positions in the later value gives the earlier value. No other pair works, so there are three valid pairs.

Constraints

  • Each value in nums is a non-negative integer.
  • Values are written in their usual decimal form. Leading zeros created by a swap do not change the resulting integer value.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 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
  • 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

  • Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
  • Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
  • Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
  • Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
  • Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions

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
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long countDistinctPairs(int[] nums) {
        // Write your code here.
    }
}
Checking account…