Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Three-Element Target Combinations

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

Your question is Three-Element Target Combinations. 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

Warner Music Group may need to identify three compatible catalog attributes whose numeric scores reach a required total. Given an array of integers and a target value, return every unique combination of exactly three elements whose sum equals the target.

Formal Specification

Implement three_sum_target(nums, target).

  • nums is a list of integers.
  • target is an integer.
  • Return a list of triplets, where each triplet contains three values from distinct array positions and sums to target.
  • Return each value combination only once, even when duplicate values produce the same triplet.
  • Each triplet must be in nondecreasing order, and the returned list may be ordered lexicographically.
  • A value may appear more than once in a triplet only when it occurs at least that many times in nums.

You may reorder nums in place.

Constraints

  • 3 <= len(nums) <= 2,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Triplets must use three distinct array positions
  • Duplicate value triplets must appear only once

Function Signature

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