4Sum
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given an integer array nums and an integer target, find every **unique** quadruplet [nums[a], nums[b], nums[c], nums[d]] such that all four indices a, b, c, and d are distinct and the four chosen values sum exactly to target.
Return a list of all such quadruplets. The result may be returned in any order, but it must not contain duplicate quadruplets. Two quadruplets are considered the same if they consist of the same multiset of values (the same numbers in the same counts), regardless of the order in which those values were drawn from the array.
For example, if both [-1, 0, 0, 1] and [0, -1, 1, 0] could be formed from the array, they count as the **same** quadruplet and should appear only once.
The values inside each returned quadruplet may be listed in any order. Aim for an efficient approach rather than checking all four-element combinations explicitly.
Examples
Constraints
- `0 <= nums.length <= 200`
- `-10^9 <= nums[i] <= 10^9`
- `-10^9 <= target <= 10^9`
- The sum of any four elements is guaranteed to fit in a 64-bit signed integer.
- Each quadruplet in the output must be unique.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.