PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates competency in array algorithms, combinatorial search, and handling duplicates with index-uniqueness constraints, and falls under the Coding & Algorithms domain.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Machine Learning Engineer

Find all quadruplets summing to target

Company: Snapchat

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given an integer array `nums` (length `n`) and an integer `target`, return **all unique quadruplets** `[a,b,c,d]` such that: - `a + b + c + d == target` - The quadruplet uses **four distinct indices** from `nums` - The output contains **no duplicate quadruplets** (order of quadruplets and order within a quadruplet do not matter) ## Input / Output - **Input:** `nums: int[]`, `target: int` - **Output:** `List<List<int>>` of unique quadruplets ## Constraints (typical interview) - `0 <= n <= 2000` - Values may be negative and may repeat ## Example - `nums = [1,0,-1,0,-2,2]`, `target = 0` - Output could be: `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]` ## Clarifications - If no quadruplet exists, return an empty list. - Duplicates in `nums` are allowed, but duplicates in the returned quadruplets are not.

Quick Answer: This question evaluates competency in array algorithms, combinatorial search, and handling duplicates with index-uniqueness constraints, and falls under the Coding & Algorithms domain.

Given an integer array nums and an integer target, return all unique quadruplets [a, b, c, d] such that a + b + c + d == target. Each quadruplet must use four distinct indices from nums. Duplicates in nums are allowed, but duplicate quadruplets must not appear in the output. For deterministic results, return each quadruplet in nondecreasing order and return the list of quadruplets in lexicographic order.

Constraints

  • 0 <= len(nums) <= 2000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Values may be negative and may repeat
  • The number of returned quadruplets is manageable for the test data

Examples

Input: ([1, 0, -1, 0, -2, 2], 0)

Expected Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]

Explanation: These are the three unique quadruplets that sum to 0.

Input: ([], 0)

Expected Output: []

Explanation: An empty array cannot provide four distinct indices.

Input: ([0, 0, 0, 0, 0], 0)

Expected Output: [[0, 0, 0, 0]]

Explanation: Although there are multiple ways to choose indices, there is only one unique quadruplet by value.

Input: ([2, 2, 2], 8)

Expected Output: []

Explanation: The values could sum to 8 only with four 2s, but there are only three elements, so four distinct indices are impossible.

Input: ([-2, -1, -1, 0, 0, 1, 1, 2], 0)

Expected Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-2, 0, 1, 1], [-1, -1, 0, 2], [-1, -1, 1, 1], [-1, 0, 0, 1]]

Explanation: The array contains duplicates, so quadruplets like [-1, -1, 1, 1] are valid, but each unique value combination appears only once.

Hints

  1. Handle duplicates by thinking in terms of value counts rather than raw indices.
  2. A quadruplet can be viewed as two pairs whose sums add up to target. Store valid value-pairs by their sum, then combine complementary sums carefully.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Determine Whether Courses Can Be Completed - Snapchat (medium)
  • Solve Decimal Coin Change - Snapchat (medium)
  • Find Maximum Island Perimeter - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a Timestamped Counter - Snapchat (medium)