Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Anna refuses to pay for one item she did not eat. Her fair share is half of the bill minus that one item: (total − bill[k]) / 2. If the amount Brian charged her, b, equals that fair share, print "Bon Appetit"; otherwise she was overcharged and you print the refund b − fairShare. Summing the bill is O(n).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you compute a fair split by removing the excluded item before halving, and correctly branch between the exact-charge ('Bon Appetit') and overcharge (print refund) cases — simple arithmetic with an easy off-by-one in the order of operations.
Where candidates slip
Halving the full total and then subtracting, or subtracting bill[k] after dividing, both of which compute the wrong fair share. Also printing the wrong string casing.
How to stand out
State the fair share as 'half of everything except item k', compute it in one pass, and note the refund is b − fairShare only when they differ.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewWas Anna overcharged?b == fair?YesNoExplore the full treeCollapse the tree
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 2
Skip-in-Loop — Sum Everything Except k
1Hint 1Explore hintHide hint
Anna shares every item except item k, so add up all costs whose index is not k.
2Hint 2Explore hintHide hint
Halve that sum to get her fair share, since she and Brian split it evenly.
3Hint 3Explore hintHide hint
Compare to b: equal prints "Bon Appetit", otherwise print the overpayment b − fairShare.
A · Core idea
Build Anna's share directly by summing only the items she actually shared.
The split model. Anna and Brian divide the cost of the shared items equally. The shared items are everything except item k, the one Anna did not eat. So Anna's fair share is half the sum of all costs at indices other than k.
Summing with a skip. Walk the array and add bill[i] whenever i != k. That excludes the skipped item as you go, leaving the total cost of shared items. Halving it gives Anna's fair contribution.
Deciding the output. Compare the fair share with what Brian charged, b. If they are equal, the split was correct and the answer is the literal string "Bon Appetit". If b is larger, Brian mistakenly charged Anna for the skipped item; the amount he owes her back is b − fairShare.
Why a tiny refinement helps. The per-iteration if (i != k) check is harmless but slightly noisy. The optimal variant drops it by summing everything and subtracting bill[k] once — the same result with a cleaner loop. Either is O(n); mention the skip version, then show the subtraction version.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
bill = [3, 10, 2, 9], k = 1, b = 12Array state
i=0
add 3 → share 3
5C · Code with teaching comments
Reference implementation previewpublic void bonAppetit(int[] bill, int k, int b) { int annaShare = 0; for (int i = 0; i < bill.length; i++) { if (i != k) { // skip the item Anna did not eat annaShare += bill[i];Explore the full codeHide the full code
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Skipping index k inside the loop and subtracting bill[k] after a full sum compute the same fair share. Seeing that equivalence is the small 'can we simplify the loop?' observation interviewers like.
ComplexityO(n) time, O(1) space — One pass over n items with a constant-time check each, plus a few arithmetic operations — O(n) time, O(1) space.
E · Edge cases and pitfalls3Halving before subtracting / skipping item k — that computes the wrong share.Explore edge casesHide edge cases
Pitfalls
- Halving before subtracting / skipping item k — that computes the wrong share.
- Forgetting to skip index k at all and charging Anna for the item she didn't eat.
- Printing 'bon appetit' or 'Bon appetit' — the casing must match exactly.
Approach 2 of 2 · Optimal
Total Minus Item — Branchless Sum (Optimal)
1Hint 1Explore hintHide hint
Sum the whole array first — no condition in the loop.
2Hint 2Explore hintHide hint
Subtract the skipped item's cost once after the loop: total − bill[k].
3Hint 3Explore hintHide hint
Halve to get the fair share, then compare to b for the same two outcomes.
A · Core idea
Same result, simpler loop: total everything, then remove the one item.
Why the branch comes out of the loop. Instead of testing i != k on every iteration, sum the entire bill unconditionally, then subtract bill[k] exactly once. The arithmetic is identical — (total − bill[k]) is the cost of the shared items — but the inner loop is now a plain sum, which is easier to read and reason about.
Computing the fair share. Halve total − bill[k]. The problem guarantees this quantity is even, so integer division is exact and there is no rounding to worry about.
The two outcomes. If the fair share equals what Brian charged (b), print "Bon Appetit". Otherwise Anna was overcharged by b − fairShare, and that difference is the refund. Because Anna can only ever be charged the correct amount or be overcharged for item k, the difference is never negative.
Why this is the version to write. It separates the two concerns cleanly — one pass to total, one line to adjust — and avoids re-checking the same constant index n times. Functionally equal to the skip-in-loop version, just tidier.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
bill = [3, 10, 2, 9], k = 1, b = 7Interactive diagram
Sum, subtract, halve, compare
sumThe whole algorithm as a four-step pipeline.
Bon AppetitC · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Subtracting bill[k] once after a full sum is the branchless twin of skipping it inside the loop. The fair share is 'cost of everything shared, split two ways' — the order is sum, subtract, halve, compare.
ComplexityO(n) time, O(1) space — A single clean pass to total the array, then constant-time subtraction, halving, and comparison — O(n) time, O(1) space.
E · Edge cases and pitfalls6bill = [0, 0], k = 0, b = 0 — total = 0, fair = (0 − 0)/2 = 0, b = 0 → Bon Appetit. Zero-cost items are fine.Explore edge casesHide edge cases
Edge cases handled
bill = [0, 0], k = 0, b = 0total = 0, fair = (0 − 0)/2 = 0, b = 0 → Bon Appetit. Zero-cost items are fine.bill = [5, 5, 5, 5], k = 2, b = 10total = 20, fair = (20 − 5)/2 = 7 (given even-guarantee inputs this case wouldn't arise); with valid data fair matches or refund prints.bill = [4, 6, 8], k = 1, b = 6total = 18, fair = (18 − 6)/2 = 6, b = 6 → Bon Appetit.
Pitfalls
- Computing fair as total/2 − bill[k] instead of (total − bill[k])/2.
- Subtracting bill[k] after halving, which double-discounts the item.
- Mismatching the exact 'Bon Appetit' string.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void bonAppetit(int[] bill, int k, int b) { int total = 0; for (int cost : bill) { total += cost; // sum everything, no branch }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Wrong order of operations
Anna's share is half of (total − bill[k]). Halving first, or subtracting bill[k] after halving, both miscompute it.
Explore the code comparisonHide the code comparison
Wrong order of operations
Anna's share is half of (total − bill[k]). Halving first, or subtracting bill[k] after halving, both miscompute it.
Don't write this
int fair = total / 2 - bill[k]; // double-discounts the itemWrite this instead
int fair = (total - bill[k]) / 2; // remove item, then split2Not excluding item k
If you sum the whole bill and halve, Anna pays for the item she skipped — the entire point of the problem.
Explore the code comparisonHide the code comparison
Not excluding item k
If you sum the whole bill and halve, Anna pays for the item she skipped — the entire point of the problem.
Don't write this
fair = sum(bill) // 2 # includes the skipped itemWrite this instead
fair = (sum(bill) - bill[k]) // 23Wrong output string
The checker expects exactly 'Bon Appetit'. Variants like 'bon appetit' or printing 0 fail.
Explore the code comparisonHide the code comparison
Wrong output string
The checker expects exactly 'Bon Appetit'. Variants like 'bon appetit' or printing 0 fail.
Don't write this
print(0) # when fair == bWrite this instead
print("Bon Appetit") # exact string on a fair chargeRevise
Carry the pattern into the interview
Pattern to remember
Sum, subtract the skipped item, halve, compare
Code pattern to memorize
total = sum(bill)
fair = (total - bill[k]) // 2
print("Bon Appetit" if fair == b else b - fair)Rules to remember
- 1Exclude item k before halving, not after: fair share is
(total − bill[k]) / 2, the cost split of everything Anna actually shared. - 2The two split the non-k items equally, so each owes half of
total − bill[k]. - 3If the charged amount
bequals the fair share, print exactly"Bon Appetit"— string match, capital B and A. - 4Otherwise the refund is
b − fair, always non-negative here because Anna can only be overcharged for the item she skipped. - 5Integer division is safe — the problem guarantees
total − bill[k]is even so the split is exact.
Use this pattern when
- Split a total fairly after removing specific items
- Compare an actual charge against a computed expected value
- Any 'who owes what' after one party opts out of a line item
- A single linear pass to total then a constant-time adjustment
Choose another pattern when
- Multiple opted-out items with different owners → track per-person sums
- Uneven or weighted splits → not a plain halving
- You need running totals over queries → prefix sums
- Tax / tip percentages layered on → more arithmetic than one subtraction
Interview takeaway
Fair-share problems are: total everything, remove what doesn't apply, divide by the number of payers, then compare to what was charged. Bon Appétit is the two-person, one-skipped-item case — (total − bill[k]) / 2 versus b.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternFair-share computation. Total the relevant costs, remove what doesn't apply, divide among the payers, then compare to the actual charge. Bon Appétit is the minimal instance: (total − bill[k]) / 2 versus b, decided in one linear pass.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Subarray DivisionAnother array-sum problem — count fixed-size windows with a target sum.
- Divisible Sum PairsPairwise arithmetic over an array, similar simple O(n) reasoning.
- Drawing BookComing soonO(1) arithmetic modelling of a real-world scenario.