machine coding

Parking Lot

The classic first round: floors, spot types, nearest free spot and an hourly bill in ₹.

Easy90 minutesJava · class ParkingLot

the brief

What you are building

A mall in Pune is opening a multi-level parking. The owner wants a small program for the entry and exit gates.

Each floor has a row of spots. A spot fits one type of vehicle: a bike, a car or a truck.

At the entry gate, the system gives the vehicle the nearest free spot of its type. At the exit gate, it frees the spot and prints the bill.

No database, no UI. Just clean, working Java that the interviewer can run and read.

must have

Requirements

  1. Build the lot from a list of floor plans. Each plan is a string with one letter per spot: B bike, C car, T truck.
  2. Spot i on floor f has the id f-i (for example 0-3).
  3. park gives the nearest free spot of the vehicle's type: the lowest floor first, then the lowest spot number on that floor.
  4. A vehicle only fits its own type of spot. If none is free, park returns "".
  5. A vehicle that is already inside cannot park again: park returns "".
  6. leave frees the spot and returns the fee. Every started hour is paid in full, and the first hour is always paid.
  7. Rates per hour: bike ₹10, car ₹20, truck ₹50. leave for a vehicle that is not inside returns -1.
  8. whereIs finds a parked vehicle; freeSpots counts the free spots of one type.

the contract

The Java API: class ParkingLot

Times are minutes from when the lot opened, passed in by the caller. This keeps the code testable: no clock inside.

public ParkingLot(String[] floors)
Floor f's plan is floors[f], e.g. "BCC". A floor may be empty.
public String park(String vehicleNo, String type, long nowMin)
type is "BIKE", "CAR" or "TRUCK". Returns the spot id, or "".
public int leave(String vehicleNo, long nowMin)
Frees the spot. Returns the fee in ₹, or -1 if the vehicle is not inside.
public String whereIs(String vehicleNo)
The vehicle's spot id, or "" if it is not inside.
public int freeSpots(String type)
How many spots of this type are free right now.
  • 1 ≤ floors ≤ 50, each floor has 0 to 200 spots
  • times never go backwards
  • up to 10,000 calls
  • vehicle numbers are non-empty strings like KA01AB1234

example session

Calls in, answers out

Floor 0 is BCC (a bike spot, then two car spots). Floor 1 is CT.

callreturnswhy
new ParkingLot(new String[] {"BCC", "CT"})—
park("KA01AB1234", "CAR", 0)"0-1"the nearest car spot
park("MH12XY9999", "CAR", 10)"0-2"
park("DL05CD0001", "CAR", 15)"1-0"floor 0 has no car spot left
park("TN09EF4321", "CAR", 20)""every car spot is taken
freeSpots("CAR")0
leave("KA01AB1234", 90)4090 minutes = 2 started hours × ₹20
park("TN09EF4321", "CAR", 95)"0-1"the freed spot is the nearest again
whereIs("MH12XY9999")"0-2"
leave("UP16ZZ0000", 100)-1this vehicle never came in
park("KA01BK7777", "BIKE", 100)"0-0"
leave("KA01BK7777", 160)10exactly 60 minutes = 1 hour × ₹10
freeSpots("TRUCK")1

time plan

Your 90 minutes

  1. 10 minclarify — Read the brief twice. Ask: can a bike use a car spot? What if the same vehicle parks twice? Write the answers down.
  2. 10 mindesign — List the nouns: lot, floor, spot, vehicle type, ticket, pricing. Pick one data structure per question the code must answer fast.
  3. 40 mincore code — park and leave first, with the fee. Then whereIs and freeSpots. Run the example after each method.
  4. 20 minedge cases — Full lot, parking twice, leaving twice, 0 minutes, exactly 60 minutes, an empty floor.
  5. 10 minpolish — Names, small methods, remove dead code. Be ready to explain how a new vehicle type or a new price rule fits in.

Practise under real pressure: a 90-minute clock with the phases above. It keeps running if you reload the page.

your turn

Build it here

Write the whole class in the editor. Run replays the example session; Submit adds the hidden tests (edge cases and large random sessions). Stuck for 20 minutes? Open one hint at a time.

Build ParkingLot in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    The reference, the data-structure reasoning and the rubric are hidden so you can build it first. Even a half-working attempt teaches you more than reading a perfect answer.

    Opening all three hints also shows it.

    revise

    Chapters that teach these structures