Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Triplet Sum

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

Your question is Find Triplet Sum. 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

Calsoft's platform services may need to identify three compatible values whose combined score matches a requested target. Given an integer array and a target integer, find three distinct elements whose values sum to the target.

Return the original indices of any valid triplet as a list of three integers. The indices may be returned in any order. If no such triplet exists, return an empty list. An element cannot be reused, although duplicate values at different indices may be selected.

Formal Specification

Implement find_triplet(nums, target):

  • Input: nums, a list of integers, and target, an integer.
  • Output: A list containing three distinct indices whose values sum to target, or [] if no solution exists.

Constraints

  • 3 <= len(nums) <= 10^4
  • -10^9 <= nums[i], target <= 10^9
  • Indices in a returned triplet must be distinct
  • Return any valid triplet when multiple solutions exist
  • Duplicate values are allowed

Function Signature

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