Crossed alibis
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- indexes
- comparisons
Problem
"One list, and in order," says Inspector Reed. Two officers tailed the suspect, and each one wrote down, from earliest to latest, the times they saw him. The notebooks are huge and already in order: sorting it all again would throw that work away.
Write a function that takes first and second, two lists of integers sorted from smallest to largest (915 means 9:15), and returns a new list with every time from both, in order. Walk through both at once, with one index in each: compare the two times you're on, move the smaller one to the new list, and step forward only in that list. When one runs out, copy whatever is left of the other. No sorted and no .sort(). Repeated times all stay, within one list or across both.
Either list, or both, can be empty.
For example, with [800, 930, 1100] and [845, 930, 1500] you return [800, 845, 930, 930, 1100, 1500].
Examples
The one in the example
[800, 930, 1100], [845, 930, 1500] → [800, 845, 930, 930, 1100, 1500]
Several in a row from one list
[100, 400, 700, 2300], [200, 300, 500, 600, 800] → [100, 200, 300, 400, 500, 600, 700, 800, 2300]
The first one is empty
[], [700, 1200] → [700, 1200]
The first one runs out early
[600], [700, 800, 900, 1000, 1100] → [600, 700, 800, 900, 1000, 1100]
Repeats in both
[900, 900, 1000], [900, 1000] → [900, 900, 900, 1000, 1000]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def merge_times(first, second):
passJavaScript
function mergeTimes(first, second) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.