PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array-based algorithm design, handling of duplicate results, and analysis of time and space complexity when producing unique triplets that sum to zero.

  • medium
  • NVIDIA
  • Coding & Algorithms
  • Software Engineer

Find all unique triplets summing to zero

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given an integer array `nums`, return **all unique triplets** `[nums[i], nums[j], nums[k]]` such that: - `i`, `j`, and `k` are **distinct indices** (`i != j != k`), and - `nums[i] + nums[j] + nums[k] == 0`. The returned triplets must be **unique** (i.e., no duplicate triplets in the output). Triplets can be returned in any order. ## Input - `nums`: an array of integers ## Output - A list/array of unique triplets, each triplet containing three integers whose sum is zero ## Constraints (typical interview bounds) - `0 <= nums.length <= 3000` - `-10^5 <= nums[i] <= 10^5` ## Example - Input: `nums = [-1, 0, 1, 2, -1, -4]` - Output: `[[-1, -1, 2], [-1, 0, 1]]`

Quick Answer: This question evaluates array-based algorithm design, handling of duplicate results, and analysis of time and space complexity when producing unique triplets that sum to zero.

Given an integer array `nums`, return all unique triplets `[nums[i], nums[j], nums[k]]` such that `i`, `j`, and `k` are distinct indices and `nums[i] + nums[j] + nums[k] == 0`. To make the output deterministic, return each triplet in non-decreasing order, and return the full list of triplets in lexicographic order. Do not include duplicate triplets.

Constraints

  • 0 <= len(nums) <= 3000
  • -100000 <= nums[i] <= 100000

Examples

Input: ([-1, 0, 1, 2, -1, -4],)

Expected Output: [[-1, -1, 2], [-1, 0, 1]]

Explanation: The only unique triplets that sum to zero are [-1, -1, 2] and [-1, 0, 1].

Input: ([0, 0, 0, 0],)

Expected Output: [[0, 0, 0]]

Explanation: Even though there are multiple ways to choose three zeros, they all form the same triplet, so it appears once.

Input: ([],)

Expected Output: []

Explanation: An empty array cannot contain any triplet.

Input: ([1, 2, -2, -1],)

Expected Output: []

Explanation: No combination of three numbers sums to zero.

Input: ([-2, 0, 1, 1, 2],)

Expected Output: [[-2, 0, 2], [-2, 1, 1]]

Explanation: The two unique triplets summing to zero are [-2, 0, 2] and [-2, 1, 1].

Solution

def solution(nums):
    nums.sort()
    result = []
    n = len(nums)

    for i in range(n - 2):
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        if nums[i] > 0:
            break

        left, right = i + 1, n - 1
        while left < right:
            total = nums[i] + nums[left] + nums[right]

            if total == 0:
                result.append([nums[i], nums[left], nums[right]])
                left += 1
                right -= 1

                while left < right and nums[left] == nums[left - 1]:
                    left += 1
                while left < right and nums[right] == nums[right + 1]:
                    right -= 1
            elif total < 0:
                left += 1
            else:
                right -= 1

    return result

Time complexity: O(n^2). Space complexity: O(n) in Python due to sorting overhead, excluding the returned result.

Hints

  1. Sorting the array helps you avoid duplicates and makes it possible to use a two-pointer scan.
  2. Fix one number, then look for two remaining numbers whose sum equals its negation.
Last updated: May 18, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute the Final Robot Score - NVIDIA (easy)
  • Return all file paths via DFS - NVIDIA (easy)
  • Implement a disk space manager with eviction - NVIDIA (medium)
  • Implement encode/decode for list of strings - NVIDIA (easy)
  • Implement short algorithms on logs, grids, and strings - NVIDIA (hard)