Interleaved orders
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- indexes
- loops
Problem
Valeria runs the diner's kitchen alone, and two lines of orders reach her: one from the dining room and one from the patio. So nobody waits too long, she takes them one and one, always starting with the dining room.
Write a function that takes dining and patio, two lists of texts, and returns a new list with the orders in the order Valeria cooks them: the first from the dining room, the first from the patio, the second from the dining room, the second from the patio, and so on. The lists can have different lengths: when one runs out, whatever is left in the other follows, in its order. If the dining room is empty, all the patio orders go. Either list, or both, can be empty, and a repeated order is cooked as many times as it shows up.
For example, with ["soup", "tacos", "pie"] and ["coffee"] you return ["soup", "coffee", "tacos", "pie"].
Examples
The one in the example
["soup", "tacos", "pie"], ["coffee"] → ["soup", "coffee", "tacos", "pie"]
Same length
["rice", "bread"], ["tea", "juice"] → ["rice", "tea", "bread", "juice"]
The patio has more
["chili"], ["water", "burger", "ice cream"] → ["chili", "water", "burger", "ice cream"]
The dining room is empty
[], ["sandwich", "lemonade"] → ["sandwich", "lemonade"]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def interleave(dining, patio):
passJavaScript
function interleave(dining, patio) {
}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.