A checkout must pick discount bundles over a cart — how do you decide greedy, divide-and-conquer or DP?
answer
- Three properties, asked in a fixed order
- Can anything span the cut you propose?
- Do two small bundles beat one big one?
- Same remaining cart via different orders
- Overlap permits a table; state size affords it
basics
~20 sThree structural tests, in order: can the cart split so no bundle spans the cut, is the largest applicable bundle always in some optimum, do different bundle orders reach the same remaining cart. They pick divide-and-conquer, greedy, and a tabulated recurrence.
solid answer
~50 sI would drive it from structure, not from instinct. First: can the cart be cut into groups no bundle spans? If yes, the pieces are independent, I solve each separately and add — plain divide-and-conquer with no table, and a cache there would never hit. If not, I test the greedy-choice property: is taking the largest-saving applicable bundle always consistent with some optimal basket? Usually it is not, because two cheap bundles can consume the items a bigger one needed. That leaves a recurrence over "best saving achievable on this remaining cart", which is dynamic programming *only if the remaining cart has a compact description* — a count vector over a handful of item categories is fine, an arbitrary subset of thousands of distinct items is not. When the state does not compress, the honest answer is search with pruning and a time budget, not a table.
go deeper
Be ready to name the three paradigms and the one property each needs: independent pieces, a safe local choice, repeated subproblems. Knowing which question to ask matters more than the answer here.
Explain why two small bundles beating one large one destroys the greedy choice, and why repeated remaining-cart states are what a table exploits. Walk the three checks in order rather than guessing.
Show that overlap only licenses a table while state-space size decides affordability. Expect to be pushed on real cart-size distributions, latency budgets, and what you do above the size threshold.
Own the policy shape: exact method below a measured size threshold, bounded search above it, threshold derived from the latency budget. Also own the risk that an assumed independent decomposition is silently wrong.
## Drive the choice from structure, in a fixed order The three paradigms are not three styles to pick by taste. Each one is licensed by a specific property, and asking about those properties in order gets you to an answer you can defend. | Question about the problem | If yes | | --- | --- | | Can the input be cut so no bundle spans the cut? | Divide-and-conquer: solve pieces independently, add, no table | | Is the locally best bundle always in *some* optimal basket? | Greedy — once you can prove it | | Do different bundle orders reach the same remaining cart? | A recurrence over that remaining cart, tabulated | ## Step one: look for independence A checkout cart is often not one problem. If bundles are scoped per merchant, per category, or per shipment, then items in different groups can never appear in the same bundle, and the groups do not interact at all. That is textbook divide-and-conquer: split, solve, combine by addition. The subproblems are disjoint, so there is nothing for a table to remember, and adding one buys nothing but memory. This step is worth doing first because it is the cheapest possible win and it is frequently available in real systems. It also shrinks whatever remains: if only one group is entangled, you can afford a much more expensive method on that group alone. ## Step two: test the greedy choice honestly The tempting rule is "apply the bundle with the largest saving that the cart still supports, then repeat". The property it needs is that some optimal basket of bundles contains that first bundle. It usually fails, and the failure has a recognisable shape: the big bundle consumes items that two smaller bundles would each have needed, and the two together saved more. Any structure where bundles *compete for the same items* invites this. So either you can argue the property — a total ordering on bundles under which the top choice is never regretted, which does exist for some pricing schemes — or you cannot, and greedy is off the table as a correctness claim. A rule you cannot justify may still ship as a heuristic, but that is a separate decision made with error measurements, not the same as choosing greedy as *the* paradigm. ## Step three: check that the state compresses If bundles compete, then different orders of applying bundles land on the same remaining cart, and that repetition is exactly the overlap that a table exploits. But overlap alone is not enough to make dynamic programming *practical*. The deciding question is how big the state space is. - If bundles are defined over a small number of item **categories**, the remaining cart is a vector of counts — a few dimensions, each bounded by the quantity in the cart. That is a modest table, and the recurrence "best saving on this count vector" is straightforward. - If bundles are defined over arbitrary sets of individually distinct items, the remaining cart is an arbitrary subset. The number of states is exponential in the number of distinct items, and no table survives a real cart. This is the step candidates skip, and it is the one that separates a senior answer from a textbook one. Overlap tells you a table *would* help; state-space size tells you whether you can afford one. When it does not compress, the real answer is branch-and-bound or beam search with a deadline and a best-so-far answer, with the exact method reserved for small carts. ## Say what you would measure before committing Before choosing, get the numbers that decide it: the distribution of cart sizes, how many distinct bundle definitions are active, whether bundles overlap in items at all, and what the checkout latency budget is. A shop where 99% of carts hold under a dozen line items and bundles are per-category can run the exact recurrence comfortably; the same code on a wholesale order of thousands of lines will not. That is why the answer is often *both*: exact below a size threshold, a fallback above it, with the threshold picked from the latency budget rather than from a feeling. ## The two failure modes to name out loud Announcing "this is dynamic programming" because the problem is an optimisation problem is the first. Optimisation is not the test; repeated subproblems over a compact state are. Assuming independence because the pieces *look* separable is the second. If one bundle can span two groups you had assumed were independent, the divide-and-conquer decomposition is silently wrong — and unlike a useless memo, this failure changes the answer rather than just the running time. Check the bundle definitions for cross-group reach before you cut.
- You are told bundles never share items across merchants. What changes?The cart splits into one independent problem per merchant, and the total is the sum of the parts. That is divide-and-conquer with no shared state, so no table is warranted, and it also shrinks the hardest remaining piece. Verify the claim against the bundle definitions first — an accidental cross-merchant bundle makes the decomposition return a wrong answer, not just a slow one.
- Overlap exists but the remaining-cart state is an arbitrary subset of distinct items. Now what?A table is licensed in theory and unaffordable in practice, since states are exponential in distinct items. Use branch-and-bound or beam search with a deadline and a best-so-far answer, and reserve an exact method for small carts. Say explicitly that you are trading optimality for a latency bound rather than pretending the table scales.
- How would you defend the decision to a reviewer who prefers the simpler greedy version?By naming the property greedy needs and showing a concrete cart where it breaks: a large bundle that consumes items two smaller bundles each required. Then quantify — how often that pattern appears in real carts and what the average saving gap is. If the gap is negligible and latency is tight, the reviewer may be right, but the decision is now evidence-based.
saying these in an interview costs you the question
- It is an optimisation problem, so it must be dynamic programming
- Assuming groups are independent without checking bundle definitions
- Picking greedy because the largest discount is obviously best
- Ignoring how large the state space actually gets
- Treating a table as free once overlap is confirmed
- Choosing a paradigm before knowing the cart-size distribution