Does this basket ship free
The storefront shows a "free shipping" badge, but the rule has an exception for heavy baskets that finance insisted on.
- Baskets at or above the threshold ship free.
- Except that anything over 20 kg never ships free, whatever it cost.
- An empty basket does not ship free.
shipsFree(basketTotal: int, grams: int, threshold: int) → bool
C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
bool shipsFree(int basketTotal, int grams, int threshold) {
}
Worked examples
| Call | Result |
|---|---|
shipsFree(50000, 3000, 30000) | true |
shipsFree(50000, 25000, 30000) | false |
shipsFree(30000, 1000, 30000) | true |
shipsFree(29999, 1000, 30000) | false |
Hint
The weight rule overrides the value rule, so check it second — or check it first and return early.
Reference solution in C++
bool shipsFree(int basketTotal, int grams, int threshold) {
if (basketTotal <= 0 || grams > 20000) return false;
return basketTotal >= threshold;
}