Burger Truck Test Orders
A burger truck lets customers add any of its extra toppings, each at most once. Before opening day, the owner wants to ring up every possible burger to check the till's prices: the plain burger, every burger with a single topping, every pair of toppings, and so on up to the burger with everything.
Burgers with the same toppings are the same burger, whatever order the toppings went on. Your job is to produce the full test list.
Your task
Write allOrders(toppings). toppings is a list of different topping names. Return a list containing every possible selection of toppings exactly once, including the empty selection (the plain burger).
Each selection is a list of topping names. The selections can be in any order, and the toppings inside a selection can be in any order.
allOrders(toppings: string[]) → string[][] Several answers can be correct; any valid one is accepted.
Examples
toppings = ["cheese", "onion", "pickle"]
[[], ["pickle"], ["onion"], ["onion", "pickle"], ["cheese"], ["cheese", "pickle"], ["cheese", "onion"], ["cheese", "onion", "pickle"]]
Each topping is on or off, so 3 toppings give 2 × 2 × 2 = 8 burgers. The plain burger [] counts, and ["onion", "cheese"] is the same burger as ["cheese", "onion"], so it must not appear twice.
toppings = []
[[]]
With no toppings there is still one burger to ring up, the plain one. The result is a list holding one empty selection, not an empty list.
Constraints
- 0 ≤ toppings.length ≤ 10
- All topping names are different, non-empty strings.
- Selections and the toppings inside them may be returned in any order.
Start with brute force
The first instinct is nested loops: one for singles, two for pairs, three for triples. But the number of loops would have to match the number of toppings, which changes with every input. Recursion gives you one level of looping per topping without fixing the number of levels in advance.
Speed is not the issue, because the answer holds 2n selections and so nothing beats O(n · 2n). Counting in binary from 0 to 2n − 1, with each bit meaning that a topping is in or out, is just as fast. Recursion is worth learning because its skeleton still works when later problems restrict which choices are allowed.
Hints
0 of 4 shown- Hint 1
List every burger for the menu
["cheese", "onion"]by hand. For each one, what did you decide about cheese, and then about onion? - Hint 2
Every burger comes from one yes/no decision per topping, in menu order: 3 toppings, 2 options each, 8 burgers. The decisions form a tree that splits in two at each topping.
- Hint 3
Write a recursive helper
choose(i)that decides toppingi: explore the burgers without it, then with it, each time moving on toi + 1. Whenireaches the end of the menu, you have one finished burger. - Hint 4
Keep one shared list
current. For "with topping i", push it, callchoose(i + 1), then pop it to restore the list. Wheniequals the menu length, store a copy ofcurrent, because the list keeps changing afterwards.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Listing every burger becomes making one yes/no decision per topping. Recursion walks the tree of decisions, and one shared list holds the toppings chosen on the current path.
Trace ["cheese", "onion"], with one step for each path of decisions:
- Decide no for cheese and no for onion. The end of the menu is reached, so record
[]. - Back up to onion, push onion, record
["onion"], and pop it. - Back up to cheese and push cheese. Decide no for onion and record
["cheese"]. - Push onion, record
["cheese", "onion"], pop onion, then pop cheese.
This finds four burgers, each exactly once, and current ends empty. Two lines make this work. The first is the copy made when recording, because without it every stored burger would be the same list, which ends empty. The second is the pop after the "with" branch, because without it toppings would leak into later burgers.
function allOrders(toppings) {
const orders = [];
const current = []; // the burger being built
function choose(i) {
if (i === toppings.length) {
orders.push([...current]); // copy: current keeps changing
return;
}
choose(i + 1); // burger without topping i
current.push(toppings[i]);
choose(i + 1); // burger with topping i
current.pop(); // undo before going back up
}
choose(0);
return orders;
}def all_orders(toppings):
orders = []
current = [] # the burger being built
def choose(i):
if i == len(toppings):
orders.append(current[:]) # copy: current keeps changing
return
choose(i + 1) # burger without topping i
current.append(toppings[i])
choose(i + 1) # burger with topping i
current.pop() # undo before going back up
choose(0)
return ordersRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.