FastPrepFind All Duplicates by Swapping
Problems/Find All Duplicates by Swapping

Find All Duplicates by Swapping

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report wants all duplicates in O(n) time and O(1) extra space, with values in [0,n-1]. It allows swaps but forbids arithmetic marking, recursion, and helper structures. We fixed that each value appears at most twice, and set the output order. Our version is about 91% the same as the reported question.

You are given a mutable integer array nums of length n. Every value is in [0,n-1], and every value appears once or twice.

Rearrange the array only by swapping elements, then return every value that appears twice. List duplicates in the order in which their extra occurrences appear during a final left-to-right scan.

Your algorithm must run in O(n) time and use O(1) auxiliary space excluding the returned array. Do not use arithmetic marking, recursion, helper data structures, or additional function calls from the primary method.

Function

findAllDuplicatesBySwapping(nums: int[]) → int[]

Examples

Example 1

nums = [2,3,1,0,2,3]return = [2,3]

Cyclic swaps place 0 through 3 at their matching indices; the remaining positions hold the extra 2 and 3.

Example 2

nums = [0,1,2,3]return = []

Every value already occupies its own index, so there is no duplicate.

Example 3

nums = [1,1,0]return = [1]

One copy of 1 reaches index 1; the other remains as the extra occurrence.

Constraints

  • 1 <= nums.length <= 200000.
  • 0 <= nums[i] < nums.length.
  • Every value appears once or twice.

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

  • Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
  • Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
  • Spiral MatrixAthenahealth · ONSITE INTERVIEW
  • Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
  • Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot 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 int[] findAllDuplicatesBySwapping(int[] nums) {
        // Write your code here.
    }
}
Checking account…