Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Triplets Sum to Zero
00:00
5 left

Triplets Sum to Zero

MediumPython

Problem

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Return only unique triplets, with each triplet sorted in nondecreasing order and the result sorted lexicographically. The input array may be modified. Return an empty list when no valid triplet exists.

Function signature: def three_sum(nums):

Input is a list of integers. Output is a list of lists of three integers.

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5
  • Each output triplet must contain values in nondecreasing order
  • The output must contain no duplicate triplets

Function Signature

def three_sum(nums):
Interviewer

Your question is Triplets Sum to Zero. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.