As reported: Samsung R&D Senior Engineer (2025), technical round with two interviewers: find triplets with sum 0 in an array — brute force first, then two pointers, then further optimisation.
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i, j, k are distinct indices and nums[i] + nums[j] + nums[k] == 0. The answer must not contain duplicate triplets.
This is LeetCode 15 — 3Sum.
Example 1
nums = [-1,0,1,2,-1,-4] -> [[-1,-1,2],[-1,0,1]]
Example 2
nums = [0,1,1] -> []
Example 3
nums = [0,0,0] -> [[0,0,0]]
Constraints: 3 ≤ nums.length ≤ 3000; -10^5 ≤ nums[i] ≤ 10^5.
Code runner format: one line: nums as a JSON array. Implement threeSum(nums); the harness sorts each triplet and the list of triplets before printing, so any order is accepted.