Return the K Smallest Pairwise Products of Two Sorted Arrays

Quick Overview

Given two sorted arrays of non-negative integers and an integer k, return the k smallest values among all pairwise products, counted with multiplicity and listed in non-decreasing order. It tests reasoning about ordered search spaces, duplicate products and products beyond the 32-bit range.

Return the K Smallest Pairwise Products of Two Sorted Arrays

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given two integer arrays `nums1` and `nums2`, each sorted in non-decreasing order, and an integer `k`. Every pair of indices `(a, b)` with `0 <= a < len(nums1)` and `0 <= b < len(nums2)` forms the product `nums1[a] * nums2[b]`, so there are `len(nums1) * len(nums2)` products in total. Return the `k` smallest of them. ### Function Signature ```python def k_smallest_products(nums1: list[int], nums2: list[int], k: int) -> list[int]: ``` ### Rules - Products are counted with multiplicity: two different index pairs contribute two products even when their values are equal, and equal values within one array are separate elements. - Return exactly `k` values in non-decreasing order. Only values are returned, so the answer is unique. ### Constraints - `1 <= len(nums1) <= 10^4` and `1 <= len(nums2) <= 10^4` - `0 <= nums1[a] <= 10^5` and `0 <= nums2[b] <= 10^5`; both arrays are sorted in non-decreasing order - `1 <= k <= min(len(nums1) * len(nums2), 10^4)` - A product can be as large as `10^10`, which exceeds `2^31 - 1`; languages with fixed-width integers need 64-bit arithmetic. ### Examples **Example 1** ```text Input: nums1 = [1, 2, 4], nums2 = [1, 3, 5], k = 4 Output: [1, 2, 3, 4] ``` The nine products in sorted order are 1, 2, 3, 4, 5, 6, 10, 12 and 20. **Example 2** ```text Input: nums1 = [2, 2], nums2 = [3, 4], k = 3 Output: [6, 6, 8] ``` Each of the two 2s pairs with 3, giving the product 6 twice; the next smallest product is 8. **Example 3** ```text Input: nums1 = [0, 3], nums2 = [2, 7, 9], k = 5 Output: [0, 0, 0, 6, 21] ``` The 0 in `nums1` makes three products equal to 0, followed by 6 and 21; the largest product, 27, is excluded.

Overview: Given two sorted arrays of non-negative integers and an integer k, return the k smallest values among all pairwise products, counted with multiplicity and listed in non-decreasing order. It tests reasoning about ordered search spaces, duplicate products and products beyond the 32-bit range.

|Home/Coding & Algorithms/LinkedIn
LinkedIn logo
LinkedIn
Sep 24, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

You are given two integer arrays nums1 and nums2, each sorted in non-decreasing order, and an integer k. Every pair of indices (a, b) with 0 <= a < len(nums1) and 0 <= b < len(nums2) forms the product nums1[a] * nums2[b], so there are len(nums1) * len(nums2) products in total. Return the k smallest of them.

Function Signature

def k_smallest_products(nums1: list[int], nums2: list[int], k: int) -> list[int]:

Rules

  • Products are counted with multiplicity: two different index pairs contribute two products even when their values are equal, and equal values within one array are separate elements.
  • Return exactly k values in non-decreasing order. Only values are returned, so the answer is unique.

Constraints

  • 1 <= len(nums1) <= 10^4 and 1 <= len(nums2) <= 10^4
  • 0 <= nums1[a] <= 10^5 and 0 <= nums2[b] <= 10^5 ; both arrays are sorted in non-decreasing order
  • 1 <= k <= min(len(nums1) * len(nums2), 10^4)
  • A product can be as large as 10^10 , which exceeds 2^31 - 1 ; languages with fixed-width integers need 64-bit arithmetic.

Examples

Example 1

Input:  nums1 = [1, 2, 4], nums2 = [1, 3, 5], k = 4
Output: [1, 2, 3, 4]

The nine products in sorted order are 1, 2, 3, 4, 5, 6, 10, 12 and 20.

Example 2

Input:  nums1 = [2, 2], nums2 = [3, 4], k = 3
Output: [6, 6, 8]

Each of the two 2s pairs with 3, giving the product 6 twice; the next smallest product is 8.

Example 3

Input:  nums1 = [0, 3], nums2 = [2, 7, 9], k = 5
Output: [0, 0, 0, 6, 21]

The 0 in nums1 makes three products equal to 0, followed by 6 and 21; the largest product, 27, is excluded.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...