Quick Overview

Merge two unsorted integer arrays into the reserved space of the first array and sort the combined values in place. Learn how length, not placeholder values, identifies real elements and analyze the sort-based time and space costs.

Merge Two Unsorted Arrays into the First Array

Company: Fora Travel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Merge Two Unsorted Arrays into the First Array You are given two unsorted integer arrays, `a` and `b`. The final `len(b)` positions of `a` are placeholders, so the first `len(a) - len(b)` positions contain its real values. Merge every value from `b` into `a`, sort all real values in ascending order, and leave the result in `a`. For example: ```text a = [5, 1, 3, 0, 0, 0] b = [6, 2, 4] a becomes [1, 2, 3, 4, 5, 6] ``` The task does not require constant auxiliary space; state the space used by the approach you choose. ### Constraints & Assumptions - Both real portions are unsorted. - The placeholder suffix of `a` has exactly enough positions to hold `b`. - A zero in the real prefix of `a` is data; placeholder positions are identified by their location, not their value. - The caller observes the sorted result through the mutated array `a`. ### Clarifying Questions to Ask - Is returning `a` in addition to mutating it useful for the target language's API? - May a built-in comparison sort be used? - Are there memory restrictions beyond storing the supplied arrays? ```hint Identify the real prefix by length The number of valid elements initially in `a` is `len(a) - len(b)`, regardless of the placeholder value. ``` ```hint Exploit the relaxed space requirement Because both inputs are unsorted, first filling the reserved suffix and then sorting the full array is a direct baseline. ``` ### Evaluation Criteria - Correct use of only the real prefix of `a` before copying `b` into its reserved suffix. - Ascending ordering with duplicates and negative values handled normally. - In-place mutation of the supplied destination array. - `O((m+n) log(m+n))` time for the direct sort-based approach and an accurate statement of the sorting implementation's auxiliary space. ### Extensions to Discuss - How would the solution improve if both real input portions were already sorted? - If constant auxiliary space were required while both inputs remained unsorted, what sorting strategy and trade-offs would you consider? - How would you test that a real zero in `a` is not mistaken for a placeholder?

Quick Answer: Merge two unsorted integer arrays into the reserved space of the first array and sort the combined values in place. Learn how length, not placeholder values, identifies real elements and analyze the sort-based time and space costs.

Array a has len(b) reserved suffix positions; only its prefix of length len(a) - len(b) contains real values. Copy every value from unsorted b into that suffix, sort all real values in ascending order, mutate a, and return a for grading.

Constraints

  • 0 <= b.length <= a.length <= 5,000.
  • The first a.length - b.length positions of a are real values; the suffix values are ignored placeholders.
  • Every real value is an integer in the inclusive range [-10^12, 10^12].
  • The function must mutate a and returns that same sorted logical array for deterministic grading.

Examples

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

Expected Output: [1, 2, 3, 4, 5, 6]

Explanation: The source example fills three placeholders and sorts both unsorted portions.

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

Expected Output: [1, 2, 3]

Explanation: With no second array, every position of a is real and is sorted.

Hints

  1. Use the two lengths, not the placeholder value, to find where real data in a ends.
  2. Because both real input portions are unsorted and extra-space restrictions are absent, first fill the suffix and then sort the complete destination.

Loading coding console...