Wednesday, January 27, 2010

Amazon Interview Question: Design an OO parking lot - Stack Overflow

Amazon Interview Question: Design an OO parking lot - Stack Overflow



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!
object-oriented-design interview-questions
flag

edited Apr 19 at 6:28
ojblass
4,823●8●26

asked Apr 19 at 6:07
burnt1ce
1,124●11

54% accept rate

2

Did you leap up and exclaim "what does this have to do with books?" and storm out? – JP Alioto Apr 19 at 6:39

I got asked that by a guy who went on to another situation. When I used a nearly textbook Gang of Four pattern appropriately, he said "At least you know polymorphism." I was then thanked for coming, and told they'd let me know. I wasn't impressed. – David Thornley May 7 at 17:18
4 Answers
oldest newest votes
vote up 9 vote down check


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.
link|flag

edited Apr 19 at 6:28


answered Apr 19 at 6:22
Christopher Morley
330●1●10

2

Please don't forget car. – ojblass Apr 19 at 6:31
vote up 1 vote down


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.
link|flag

answered Apr 19 at 6:31
Paul Keister
486●1●7


vote up 0 vote down


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

link|flag

answered Apr 19 at 6:32
Scott M.
1,037●10


vote up 0 vote down


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.

1 comment: