SystemDrills

Session: Sign in to solve

Solution.txt

Apartment Water Bill

Problem

Design ApartmentWaterBill for one apartment and one fixed 30-day month. The apartment receives ordinary water from corporation and borewell sources in a fixed ratio. Guests add demand that must be supplied by progressively priced tanker water.

Requirements

  • Select the base allowance. A 2BHK apartment has 900 base litres for the month, and a 3BHK apartment has 1,500 base litres.
  • Split base water by the exact ratio. If the ratio is corporationPart:borewellPart, corporation litres are baseLitres * corporationPart / (corporationPart + borewellPart), and borewell litres are the corresponding borewell share. Keep both shares exact and do not round them.
  • Accumulate guests. The guest count starts at zero. Each addGuests(count) call increases the same month's count by count.
  • Supply guest demand from tankers. Every guest adds exactly 300 litres, all supplied as tanker water. Guest additions do not change the corporation or borewell quantities.
  • Price contracted sources. Corporation water costs 1 charge unit per litre. Borewell water costs 1.5 charge units per litre. Apply each rate to its exact share.
  • Price tanker water progressively. Charge the first 500 tanker litres at 2 per litre, the next 1,000 at 3 per litre, the next 1,500 at 5 per litre, and every litre above 3,000 at 8 per litre. Each rate applies only to litres inside its band.
  • Calculate total litres. getBill() returns totalLitres equal to the base litres plus 300 times the accumulated guest count.
  • Aggregate the raw charge exactly. Add the exact corporation, borewell, and tanker charges. Do not round any source quantity or line-item charge before this sum.
  • Round only the final charge. getBill() returns totalCharge by rounding the raw aggregate charge to the nearest integer. An exact half rounds upward.
  • Accept positive guest additions. addGuests(count) returns true for a positive count and adds it to the cumulative guest count.
  • Reject nonpositive guest additions. addGuests(count) returns false when count is zero or negative.
  • Keep rejected additions atomic. A rejected addGuests call does not change cumulative guests. A later getBill() returns the same result it would have returned if the rejected call had not occurred.

API

SignatureReturnsBehavior
ApartmentWaterBill(apartmentType: string, corporationPart: integer, borewellPart: integer)Not applicableCreates the apartment-month with zero guests.
addGuests(count: integer)booleanReturns true and adds a positive count. Returns false without changing state for zero or a negative count.
getBill()BillResultReturns an object with exactly totalLitres: integer and totalCharge: integer.

Examples

For ApartmentWaterBill("2BHK", 3, 7):

StepOperationResult
1addGuests(2)true
2addGuests(3)true
3getBill(){"totalLitres":2400,"totalCharge":5215}

The 900 base litres split into 270 corporation litres and 630 borewell litres, costing 270 and 945. Five guests add 1,500 tanker litres, costing 500 * 2 + 1,000 * 3 = 4,000. The total is 2,400 litres and 5,215 charge units.

Constraints

  • apartmentType is exactly 2BHK or 3BHK.
  • 1 <= corporationPart <= 1,000,000
  • 1 <= borewellPart <= 1,000,000
  • Each addGuests call satisfies -1,000,000 <= count <= 1,000,000.
  • At most 100,000 public method calls occur per testcase.
  • The cumulative guest count is at most 1,000,000,000.
  • All exact intermediate numerators fit in a signed 64-bit integer.
  • Only inputs satisfying these constraints are supplied and judged.

Notes

Calls are sequential on one object. Meter readings, corrections, tariff changes, statements, payment, month reset, invalid constructor behavior, out-of-range guest counts, persistence, and concurrent calls are outside scope.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.