← MathWorks Interview Insights

MathWorks·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

MathWorks SET interview with an array pairing problem that looks straightforward until you actually think about what the target sum should be. Sorting and two pointers got me there but I fumbled a bit on the product accumulation part.

Questions Asked (1)

Q1

Given an array of even length, pair up all elements so every pair has the same sum. If valid pairings exist, return the sum of all pair products. If not, return -1.

Algorithms & Data Structures
Author's notes

Sorted the array first, then ran two pointers from each end to check if every pair hit the same target.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, sort the array and check if pairing the smallest with the largest yields a constant sum. If so, compute the sum of products of these pairs; otherwise, return -1. This approach ensures O(n log n) time and O(1) extra space.

Pro tip: After sorting, verify the constant sum condition by checking only the first and last pair; if they match, all other pairs will automatically match due to the sorted order. This avoids unnecessary checks and demonstrates efficient reasoning.

1. Sort the array

Sort the array in ascending order to easily pair smallest with largest elements.

2. Determine target sum

Compute the sum of the first and last elements as the target sum for all pairs.

3. Validate pairing

Iterate through the array from both ends, checking if each pair sums to the target. If any pair does not, return -1.

4. Compute sum of products

If all pairs are valid, calculate the product of each pair and sum them up.

5. Return result

Return the computed sum if valid, otherwise -1.

Key Points to Mention

  • Sorting is key to efficiently pairing smallest with largest.
  • The constant sum condition can be checked by comparing the first and last pair.
  • Time complexity is O(n log n) due to sorting, with O(1) extra space.
  • Edge cases: empty array (though length is even, could be 0), negative numbers, duplicates.
  • If the array length is even, pairing is possible only if the sorted condition holds.
  • The sum of products can be computed in a single pass after validation.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.