Skip to the code
cf.
+10 XP

#191 Merge Two Sorted Timelines

easy

A chat app shows messages from a local cache and new ones from the server. Both lists hold timestamps sorted ascending: cached and fresh.

Write mergeTimelines(cached, fresh) that returns one array with every timestamp from both lists, sorted ascending. Keep duplicates. Do it in one pass, the way merge sort's merge step does, rather than sorting again.

Example 1

Input: cached = [1,4,9], fresh = [2,3,10]
Output: [1,2,3,4,9,10]

Take the smaller front value each time.

Example 2

Input: cached = [5,5], fresh = [5]
Output: [5,5,5]

Duplicates are kept.

Constraints

  • 0 <= cached.length, fresh.length <= 100000
  • Both arrays are sorted ascending.
  • 0 <= timestamps <= 10^12
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).