PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in array algorithms, pairing logic, data structure selection, and complexity analysis, focusing on maximizing disjoint k-sum pairs and reasoning about space–time trade-offs.

  • medium
  • MathWorks
  • Coding & Algorithms
  • Software Engineer

Maximize disjoint k-sum pairs

Company: MathWorks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given an integer array nums and an integer k. In one operation you may remove two indices i < j if nums[i] + nums[j] == k; each index can be used at most once. Return the maximum number of operations you can perform. Design an O(n) or O(n log n) solution, discuss space–time trade-offs, and provide code in C, C++, Java, or JavaScript.

Quick Answer: This question evaluates proficiency in array algorithms, pairing logic, data structure selection, and complexity analysis, focusing on maximizing disjoint k-sum pairs and reasoning about space–time trade-offs.

You are given an integer array `nums` and an integer `k`. In one operation you may remove two indices i < j if `nums[i] + nums[j] == k`; each index can be used at most once. Return the maximum number of operations you can perform. The order in which you pick pairs does not change the maximum count, so a single linear pass that greedily matches each value with a previously-seen complement is optimal. Maintain a count of unused values seen so far; for each new value `x`, if its complement `k - x` has an unused occurrence, pair them and increment the answer, otherwise record `x` as available. Design an O(n) or O(n log n) solution and discuss the space–time trade-offs.

Constraints

  • 1 <= nums.length (problem-level); the function must also handle nums.length == 0 by returning 0
  • Values and k may be negative or zero
  • Each index may participate in at most one operation
  • 1 <= nums.length <= 10^5 (typical interview bound)

Examples

Input: ([1, 2, 3, 4], 5)

Expected Output: 2

Explanation: Pair (1,4) and (2,3), both summing to 5. Two operations, no index reused.

Input: ([3, 1, 3, 4, 3], 6)

Expected Output: 1

Explanation: Only 3+3 = 6 works; there are three 3's, so one pair is formed and the remaining 3, plus 1 and 4, cannot be paired.

Input: ([], 5)

Expected Output: 0

Explanation: Empty array — no pairs possible, return 0.

Input: ([5], 5)

Expected Output: 0

Explanation: A single element cannot form a pair.

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

Expected Output: 2

Explanation: Four 2's form two disjoint pairs (2+2 = 4 each).

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

Expected Output: 1

Explanation: Three 1's: one pair (1+1 = 2) is formed, the leftover 1 is unpaired.

Input: ([-1, -2, -3, -4, 3, 1], -3)

Expected Output: 2

Explanation: Pairs (-1,-2) and (-4,1) both sum to -3; -3 and 3 are left unpaired.

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

Expected Output: 2

Explanation: Five 0's with k = 0: 0+0 = 0, so two pairs form and one 0 is left over.

Hints

  1. The maximum number of disjoint pairs is independent of pairing order, so a greedy single pass suffices — no backtracking is needed.
  2. Keep a hash map of unused values seen so far. For each value x, look up k - x; if an unused copy exists, consume it and count one operation, otherwise store x.
  3. Self-complement values (where x == k - x, e.g. x = k/2, or x = 0 with k = 0) just pair up among themselves naturally because the map count is decremented as you match.
  4. Sorting plus two pointers (one from each end moving toward a target sum) is an O(n log n) alternative with O(1) extra space — useful when memory is tight.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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

  • Minimize shortest path by adding weight-1 edges - MathWorks (easy)
  • Maximize minimum after K decrements - MathWorks (easy)
  • How to maximize rewards with exactly k tasks - MathWorks (easy)
  • Determine Whether P's Position Is Unique - MathWorks (medium)
  • Maximize minimum value after k decrements - MathWorks (medium)