Quick Overview

Merge squared values from two sorted arrays in linear time, including negative inputs, duplicate preservation, endpoint comparisons, and overflow handling.

Merge the Sorted Squares of Two Sorted Arrays

Company: Whatnot

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given two ascending integer arrays, return one ascending array containing the square of every element from both arrays. Preserve duplicates. Start with nonnegative arrays, then support negative values as a follow-up. ### Clarifying Questions to Ask - Can either array be empty, and should all duplicate values be retained? - Can squaring overflow the input integer type? ```hint Where is the largest square? In a sorted signed array, the greatest absolute value is at one of the two ends. ``` ### What a Strong Answer Covers - Ordinary merging for nonnegative inputs. - Transforming each signed array into sorted squares using two endpoints. - Linear total time without sorting the squared output from scratch. ### Follow-up Questions - Can you fill the final output backward directly from the four input endpoints? - Why is squaring each array in place insufficient when it contains negatives?

Overview: Merge squared values from two sorted arrays in linear time, including negative inputs, duplicate preservation, endpoint comparisons, and overflow handling.

Read the full Whatnot Software Engineer interview experience this question came from

Given two nondecreasing integer arrays, `first` and `second`, return one nondecreasing array containing the square of every input value. Inputs may contain negatives and duplicates. Preserve the multiplicity of every square, even when different values have equal squares. Either array may be empty.

Constraints

  • The combined input length is at most 2^31 − 1.
  • Each input is an integer whose absolute value is at most floor(sqrt(2^53 − 1)), so each square is an exact JSON integer.

Examples

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

Expected Output: [0, 1, 4, 9, 9, 16]

Explanation: The repeated 3 contributes two copies of 9.

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

Expected Output: [0, 4, 4, 9, 16, 25]

Explanation: Negative values can produce both the largest square and ties.

Hints

  1. Two values with opposite signs and the same magnitude contribute equal squares.

Merge the Squares of Two Sorted Arrays

mediumWhatnotSoftware Engineer

Given two nondecreasing integer arrays, first and second, return one nondecreasing array containing the square of every input value. Inputs may contain negatives and duplicates. Preserve the multiplicity of every square, even when different values have equal squares. Either array may be empty.

Examples

Example 1
Input
([0, 1, 4], [2, 3, 3])
Output
[0, 1, 4, 9, 9, 16]
Notes
The repeated 3 contributes two copies of 9.
Example 2
Input
([-5, -2, 3], [-4, 0, 2])
Output
[0, 4, 4, 9, 16, 25]
Notes
Negative values can produce both the largest square and ties.

Constraints

  • •The combined input length is at most 2^31 − 1.
  • •Each input is an integer whose absolute value is at most floor(sqrt(2^53 − 1)), so each square is an exact JSON integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def merge_sorted_squares(first: list[int], second: list[int]) -> list[int]:
Sample call
merge_sorted_squares([0, 1, 4], [2, 3, 3])
Language
Case 1
Input
([0, 1, 4], [2, 3, 3])
Expected
[0, 1, 4, 9, 9, 16]
Case 2
Input
([-5, -2, 3], [-4, 0, 2])
Expected
[0, 4, 4, 9, 16, 25]

Browse More Questions

Show the approach

Approach

The greatest absolute value in any unconsumed interval of a nondecreasing array is at one of its endpoints. Keep an interval for each input. Compare the magnitudes of all available endpoints, write the largest square into the last unfilled output slot, and discard only the chosen endpoint. Intervals stay sorted after each discard. This places the largest remaining square at every step, including one copy per occurrence, and uses only a constant number of indices.

Time complexity:
O(m + n) for input lengths m and n.
Space complexity:
O(1) auxiliary space excluding output.