Skip to the code
cf.
+25 XP

#112 Merge Into the Reserved Tail

medium

A memory-tight table component keeps its row ids in a buffer rows. The first count items are real ids sorted ascending; the rest of the buffer is reserved space filled with 0s, exactly incoming.length of them. New ids arrive in incoming, also sorted ascending.

Write mergeInto(rows, count, incoming) that merges incoming into rows in place so the whole buffer is sorted ascending. Don't create another array. The tests check rows after your function runs.

Example 1

Input: rows = [2,5,9,0,0], count = 3, incoming = [1,6]
Output: [1,2,5,6,9]

The two reserved spots take the new ids, and everything ends sorted.

Example 2

Input: rows = [0,0], count = 0, incoming = [3,4]
Output: [3,4]

There were no rows yet.

Constraints

  • rows.length === count + incoming.length
  • 0 <= count, incoming.length <= 100000
  • -10^9 <= ids <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).