Amazon Interview Question
Software Engineer / DevelopersTeam: AWS
Country: United States
Interview Type: Phone Interview
any more?
class ParkingLot
{
private:
string name;
string address;
int Capacity;
int Used;
public:
// how many spaces left in the parkinglot
int GetSpaces();
// how many spaces left
// enum {compact = 0, mid, ...}
int GetSpaces(int size);
// total spaces parkinglot has
int GetCapacity();
// if the parkinglot is opened
bool IsOpened();
// return parkinglot open time of a specified date
// e.g. "8:00 AM - 10:00 PM"
string GetOpenTime(time_t date);
// fee $ per hour
int ChargeMoney();
// Get the address of parking lot
string GetAddress();
// get type of parkinglot
// enum { outdoor = 0, ... };
int GetType();
string Name();
};
My solution (Didnt take time into consideration but can take it into consideration as a part of Vehicle class and calculating it in between the parkVehicle() and releaseVehicle() classes :
public class ParkingLot
{
Vector<ParkingSpace> vacantParkingSpaces = null;
Vector<ParkingSpace> fullParkingSpaces = null;
int parkingSpaceCount = 0;
boolean isFull;
boolean isEmpty;
ParkingSpace findNearestVacant(ParkingType type)
{
Iterator<ParkingSpace> itr = vacantParkingSpaces.iterator();
while(itr.hasNext())
{
ParkingSpace parkingSpace = itr.next();
if(parkingSpace.parkingType == type)
{
return parkingSpace;
}
}
return null;
}
void parkVehicle(ParkingType type, Vehicle vehicle)
{
if(!isFull())
{
ParkingSpace parkingSpace = findNearestVacant(type);
if(parkingSpace != null)
{
parkingSpace.vehicle = vehicle;
parkingSpace.isVacant = false;
vacantParkingSpaces.remove(parkingSpace);
fullParkingSpaces.add(parkingSpace);
if(fullParkingSpaces.size() == parkingSpaceCount)
isFull = true;
isEmpty = false;
}
}
}
void releaseVehicle(Vehicle vehicle)
{
if(!isEmpty())
{
Iterator<ParkingSpace> itr = fullParkingSpaces.iterator();
while(itr.hasNext())
{
ParkingSpace parkingSpace = itr.next();
if(parkingSpace.vehicle.equals(vehicle))
{
fullParkingSpaces.remove(parkingSpace);
vacantParkingSpaces.add(parkingSpace);
parkingSpace.isVacant = true;
parkingSpace.vehicle = null;
if(vacantParkingSpaces.size() == parkingSpaceCount)
isEmpty = true;
isFull = false;
}
}
}
}
boolean isFull()
{
return isFull;
}
boolean isEmpty()
{
return isEmpty;
}
}
public class ParkingSpace
{
boolean isVacant;
Vehicle vehicle;
ParkingType parkingType;
int distance;
}
public class Vehicle
{
int num;
}
public enum ParkingType
{
REGULAR,
HANDICAPPED,
COMPACT,
MAX_PARKING_TYPE,
}
A class "ParkingLot" would be an aggregate of "ParkingSpot".
- quickpuzzlesolver December 25, 2011The other main class would be the "ParkingEntryGate" at which you need collect a "Ticket".
The ParkingEntryGate would maintain the details of the "Vehicle" and the time in.
Another good feature would be a "FreeParkingSpotBillboard" which notifies people as to which parking spots are free. The "ParkingSpot" would be associated with a unique id.
Another Entity would be the "Customer" who has a "Vehicle" and a "Ticket". There would be a method named GetCustomerDetails inside "ParkingLot" which would tell us the details of time spent inside parking for each customer by date. Every customer would have a unique id.
Basically one needs to conceptualize the various entities and make a domain driven design.
(This could be used at the Parking Entry Gate with a front end of a web application if required.)