Create Maximum Number

HardGreedymonotonic stack

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two arrays nums1 and nums2 of single digits and an integer k. Build a number with exactly k digits by choosing digits from both arrays. The digits you take from one array must keep their original relative order, but you can interleave the two arrays however you like. Return the largest possible number as an array of its k digits. Numbers of equal length are compared digit by digit from the left. k is at most nums1.length + nums2.length.

Examples

Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
Output: [9, 8, 6, 5, 3]
Take [6,5] from nums1 and [9,8,3] from nums2, then merge them into [9,8,6,5,3].
Input: nums1 = [6,7], nums2 = [6,0,4], k = 5
Output: [6, 7, 6, 0, 4]
All digits are used. At the tie on 6, taking from nums1 first exposes the 7 sooner.
Input: nums1 = [3,9], nums2 = [8,9], k = 3
Output: [9, 8, 9]
Take [9] from nums1 and [8,9] from nums2 to make [9,8,9].

Constraints

  • 1 <= nums1.length, nums2.length <= 500
  • 0 <= nums1[i], nums2[i] <= 9
  • 1 <= k <= nums1.length + nums2.length

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More monotonic stack problems