Find Unique Zero-Sum Triplets

Read the full interview experience this question came from →

Quick Overview

Find unique zero-sum triplets with distinct indices, deterministic ordering, careful duplicate suppression, and explicit time and space analysis.

Find Unique Zero-Sum Triplets

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an integer array, return every distinct triplet of values whose sum is zero. Each triplet must use three different array indices. Return duplicate value-triplets only once. Implement `three_sum(nums: int[]) -> int[][]`. ### Constraints & Assumptions - `0 <= len(nums) <= 3000`; values range from -1,000,000 through 1,000,000. - Sort values within each returned triplet in ascending order. Sort the outer list lexicographically to make the output deterministic. - Equal values may be used when they come from different indices. For example, three zeros require at least three zero elements. - You may reorder the input or sort a copy. Return values, not indices. ### Examples ```text nums = [-1,0,1,2,-1,-4] result = [[-1,-1,2],[-1,0,1]] ``` ```text nums = [0,0,0,0] result = [[0,0,0]] ``` Explain how the algorithm avoids reusing an index and suppresses duplicate triplets. Give its time and auxiliary-space complexity, distinguishing output storage and any sorted copy from the search state. ```hint Make the remaining sum monotone After fixing one value in sorted order, consider how moving either endpoint of the remaining search interval changes the pair sum. ```

Overview: Find unique zero-sum triplets with distinct indices, deterministic ordering, careful duplicate suppression, and explicit time and space analysis.

Read the full Microsoft Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Sep 10, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Given an integer array, return every distinct triplet of values whose sum is zero. Each triplet must use three different array indices. Return duplicate value-triplets only once.

Implement three_sum(nums: int[]) -> int[][].

Constraints & Assumptions

  • 0 <= len(nums) <= 3000 ; values range from -1,000,000 through 1,000,000.
  • Sort values within each returned triplet in ascending order. Sort the outer list lexicographically to make the output deterministic.
  • Equal values may be used when they come from different indices. For example, three zeros require at least three zero elements.
  • You may reorder the input or sort a copy. Return values, not indices.

Examples

nums = [-1,0,1,2,-1,-4]
result = [[-1,-1,2],[-1,0,1]]
nums = [0,0,0,0]
result = [[0,0,0]]

Explain how the algorithm avoids reusing an index and suppresses duplicate triplets. Give its time and auxiliary-space complexity, distinguishing output storage and any sorted copy from the search state.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...