As reported: DoorDash phone screen (2025); the required complexity was O(n log n).
A chef receives the day's orders as a list of order ids. He builds a new list by repeatedly removing the smallest eligible order from the current list and appending it to the new list.
An order is eligible if its id is greater than both of its immediate neighbours in the current list:
- the first order needs only to be greater than its right neighbour;
- the last order needs only to be greater than its left neighbour;
- when one order remains, it is eligible.
Order ids are unique. Return the new list — the order in which the chef takes the orders.
Example 1
orders = [3,1,5,2,4]
Output: [3,4,5,2,1]
Eligible at first: 3, 5 and 4; take 3 → [1,5,2,4]. Eligible: 5, 4; take 4 → [1,5,2]. Take 5 → [1,2], then 2, then 1.
Example 2
orders = [1,2,3,4]
Output: [4,3,2,1]
Example 3
orders = [7]
Output: [7]
Constraints: 1 ≤ orders.length ≤ 10^5; 1 ≤ orders[i] ≤ 10^9; all ids are distinct.
Code runner format: one JSON value per line: orders. Implement chefOrder(orders); the harness prints the list.