Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Triplets Sum to Zero

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Array Triplets Sum to Zero. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The Divisions maintenance group needs to detect combinations of three maintenance adjustment values that cancel out to zero. Given an integer array nums, return every unique triplet [a, b, c] such that a + b + c == 0.

Formal Specification

Implement three_sum(nums). The input is a list of integers. Return a list of triplets, where each triplet contains values from three different positions in nums. Do not return duplicate triplets, even when nums contains repeated values. Sort each triplet in nondecreasing order, and return the collection of triplets in lexicographic order. The input list may be modified.

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5
  • Triplets must use three distinct positions
  • Each triplet must be sorted in nondecreasing order
  • The output must contain no duplicate triplets

Function Signature

def three_sum(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output