FastPrepFour Sum
Problems/Four Sum

Four Sum

PeopleStrong logoPeopleStrong● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names 4Sum with optimization and dry runs. The interface, bounds, value-based uniqueness and deterministic ordering are authored; core similarity is about 90%.

You are given an integer array nums and a target. Find every group of four values whose sum equals target. The four values must come from four different array positions.

For this exercise, sort the four values in each group from smallest to largest, and sort the result list lexicographically. Return each group of values only once, even if several choices of positions produce it. Return an empty list if there is no such group. You may change the input array.

Function

fourSum(nums: int[], target: long) → int[][]

Examples

Example 1

nums = [1,0,-1,0,-2,2]target = 0return = [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

These are the three distinct sorted value quadruplets with sum zero. Their indices are distinct within each selection.

Example 2

nums = [2,2,2,2,2]target = 8return = [[2,2,2,2]]

Several index selections exist, but all produce the same value quadruplet, which appears once.

Constraints

  • For this exercise, assume 0 <= nums.length <= 200.
  • -10^9 <= nums[i] <= 10^9.
  • -4 * 10^9 <= target <= 4 * 10^9; use 64-bit sums.

More PeopleStrong problems

  • Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
  • LRU Cache OperationsONSITE INTERVIEW · Seen Mar 2026
  • Reverse Linked-List Nodes in Groups of KOA · PHONE SCREEN · Seen Mar 2026
All PeopleStrong interview questionsSee PeopleStrong hiring insights
Saved
class Solution {
    public int[][] fourSum(int[] nums, long target) {
        // Write your solution here.
    }
}
Checking account…