Amazon Interview Question: Design an OO parking lot

Oop

Oop Problem Overview


Design an OO parking lot. What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact.

Thanks!

Oop Solutions


Solution 1 - Oop

Here is a quick start to get the gears turning...

ParkingLot is a class.

ParkingSpace is a class.

ParkingSpace has an Entrance.

Entrance has a location or more specifically, distance from Entrance.

ParkingLotSign is a class.

ParkingLot has a ParkingLotSign.

ParkingLot has a finite number of ParkingSpaces.

HandicappedParkingSpace is a subclass of ParkingSpace.

RegularParkingSpace is a subclass of ParkingSpace.

CompactParkingSpace is a subclass of ParkingSpace.

ParkingLot keeps array of ParkingSpaces, and a separate array of vacant ParkingSpaces in order of distance from its Entrance.

ParkingLotSign can be told to display "full", or "empty", or "blank/normal/partially occupied" by calling .Full(), .Empty() or .Normal()

Parker is a class.

Parker can Park().

Parker can Unpark().

Valet is a subclass of Parker that can call ParkingLot.FindVacantSpaceNearestEntrance(), which returns a ParkingSpace.

Parker has a ParkingSpace.

Parker can call ParkingSpace.Take() and ParkingSpace.Vacate().

Parker calls Entrance.Entering() and Entrance.Exiting() and ParkingSpace notifies ParkingLot when it is taken or vacated so that ParkingLot can determine if it is full or not. If it is newly full or newly empty or newly not full or empty, it should change the ParkingLotSign.Full() or ParkingLotSign.Empty() or ParkingLotSign.Normal().

HandicappedParker could be a subclass of Parker and CompactParker a subclass of Parker and RegularParker a subclass of Parker. (might be overkill, actually.)

In this solution, it is possible that Parker should be renamed to be Car.

Solution 2 - Oop

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,
}

Solution 3 - Oop

Models don't exist in isolation. The structures you'd define for a simulation of cars entering a car park, an embedded system which guides you to a free space, a car parking billing system or for the automated gates/ticket machines usual in car parks are all different.

Solution 4 - Oop

In an Object Oriented parking lot, there will be no need for attendants because the cars will "know how to park".

Finding a usable car on the lot will be difficult; the most common models will either have all their moving parts exposed as public member variables, or they will be "fully encapsulated" cars with no windows or doors.

The parking spaces in our OO parking lot will not match the size and shape of the cars (an "impediance mismatch" between the spaces and the cars)

License tags on our lot will have a dot between each letter and digit. Handicaped parking will only be available for licenses beginning with "_", and licenses beginning with "m_" will be towed.

Solution 5 - Oop

you would need a parking lot, that holds a multi-dimensional array (specified in the constructor) of a type "space". The parking lot can keep track of how many spaces are taken via calls to functions that fill and empty spaces.Space can hold an enumerated type that tells what kind of space it is. Space also has a method taken(). for the valet parking, just find the first space thats open and put the car there. You will also need a Car object to put in the space, that holds whether it is a handicapped, compact, or regular vehicle.


class ParkingLot
{
Space[][] spaces;



ParkingLot(wide, long); // constructor

FindOpenSpace(TypeOfCar); // find first open space where type matches




}




enum TypeOfSpace = {compact, handicapped, regular };
enum TypeOfCar = {compact, handicapped, regular };




class Space
{
TypeOfSpace type;
bool empty;
// gets and sets here
// make sure car type
}




class car
{
TypeOfCar type;
}

class car { TypeOfCar type; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionburnt1ceView Question on Stackoverflow
Solution 1 - OopChris MorleyView Answer on Stackoverflow
Solution 2 - OopSrikant AggarwalView Answer on Stackoverflow
Solution 3 - OopPete KirkhamView Answer on Stackoverflow
Solution 4 - OopPaul KeisterView Answer on Stackoverflow
Solution 5 - OopScott M.View Answer on Stackoverflow