KatInterview

Sort Array and Compute Unique Intersection

Heaps, Sorting & SearchAuto-graded · 21 test cases
EasyAuto-gradedHeaps, Sorting & Searchsortingmerge-sorthash-set

Last updated

Tests implementing custom merge sort to sort an array and finding the sorted unique intersection of two lists.

Write a function that accepts three integer lists: nums, a, and b. The function must compute and return a list containing two results:

  1. The list nums sorted in non-decreasing order.
  2. A sorted list of all unique elements that are present in both a and b in non-decreasing order.

Constraints

  • $0 \le \text{len}(nums) \le 10^4$
  • $0 \le \text{len}(a), \text{len}(b) \le 10^4$
  • $-10^9 \le \text{nums}[i], a[i], b[i] \le 10^9$
  • The original input lists must not be modified in place.

Examples

Example 1

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

Output: [[1, 2, 3], [2, 3]]

Example 2

Input: sort_numbers_and_intersect_sets([5, 5, 1, -2, 0], [7, 8], [1, 2, 3])

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

Example 3

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

Output: [[1, 2, 3, 4], [1]]

Example 4

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

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

Hints

Hint 1

Use a standard O(N log N) sorting algorithm like merge sort to avoid Python's built-in sort functions.

Hint 2

To find the intersection, consider converting one list into a set for O(1) membership lookups.

Hint 3

Ensure duplicate elements in the intersection are deduplicated before returning the sorted common elements.