A water bottle as a class
Reported in interviews at (where)
- Oro Money
Build WaterBottle, a bottle that holds up to capacity millilitres of water. Amounts are whole millilitres.
WaterBottle(capacity) makes an empty bottle.
fill(ml) pours ml millilitres in. Whatever does not fit spills, so the bottle never holds more than capacity. Returns how many millilitres went in.
drink(ml) drinks ml millilitres, or all that is left if that is less. Returns how many millilitres were drunk.
level() returns how many millilitres the bottle holds now.
isFull() returns whether the bottle holds exactly capacity.
totalDrunk() returns every millilitre drunk from this bottle since it was made.
- Input
["WaterBottle","fill","fill","isFull","drink","level","drink","level","totalDrunk","isFull"]
[[500],[300],[300],[],[150],[],[1000],[],[],[]]- Output
[null,300,200,true,150,350,350,0,500,false]
The second fill only has room for 200 of its 300 millilitres. Asking to drink 1000 drinks the 350 that are left.
- Input
["WaterBottle","drink","fill","fill","isFull","totalDrunk"]
[[1],[5],[0],[1],[],[]]- Output
[null,0,0,1,true,0]
Nothing can be drunk from an empty bottle, and pouring in 0 changes nothing.
Limits
1 <= capacity <= 10000000 <= ml <= 1000000at most 20000 calls after the constructor
Hints
Hint 1
The bottle needs to remember two numbers besides its capacity: how much it holds, and how much has been drunk in all.
Hint 2
What goes in is the smaller of ml and the room left; what is drunk is the smaller of ml and the level.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.