Wednesday, January 5, 2011

OO Design for ParkingLot

Its a parking lot design day. I am thinking of a simple parking lot where there are finite number of spaces and the space can either be a compact/handicap/regular.
A car can be parked or unparked. The parking lot can be full or empty.

I think below design will do:
ParkingLot: this class can park or unpark and holds finite number of spaces and the spaces are held by two lists one for Parkedspaces and one for unparkedSpaces
ParkingSpace: is another class that represents the actual parking space. It needs a property to specify whether it is a compact/handicap/regular parking space
Car: is the final class that has a property of what kind of car it is and if it is parked, the slot number where it is parked.
public class ParkingLot {
int totalSpaces = 100;
public static List ParkingSpace parkedSpaces = new ArrayList();
public static List ParkingSpace emptySpaces = new ArrayList();

public static void park(Car car) {
//Traverse though the emptySpaces list to find the
//spot for specific car type
//set the car object for that parkingspace
//change it to park. move the object into parkedSpaces
// remove it from emptySpaces
}

public static void unPark(int parkingSpot) {
//get the parkingspot from array
//set the parkingspace to available
}

public static boolean isEmpty() {
if (emptySpaces.size() > 0)
return true;
return false;
}

public static void main(String args[]) {
//build emptySpaces list. Insert 100 parking spaces
//with their type properties
Car car = new Car();
car.setCompact(true);
if(ParkingLot.isEmpty())
ParkingLot.park(car);

ParkingLot.unPark(20);
}
}

public class ParkingSpace {

boolean isCompact;
boolean isRegular;
boolean isHandicap;

//setter and getter calls

}

public class Car {
boolean isCompact;
boolean isRegular;
boolean isHandicap;
int parkingSlotNumber;

public Car() {
}

public void setParkingSlotNumber(int value) {
this.parkingSlotNumber = value;
}
}

The Car object can be further subclassed as CompactCar, HandiCapCar, RegularCar instead of the booleans. Same with the ParkingSpace as well.

But is there any use in that?

Please share your thoughts.

Tuesday, January 4, 2011

In an array find two numbers that add up to a given value X

As part of researching into regular math questions asked in interview, I stumbled upon this simple data structure question of "from an array of integers, given an input x, how do you find two numbers that add up to X.

The basic algorithm to find two numbers that add up to X:
declare hash
for each element in the array
diff = current_element - X
if(!hash.contains(current_element))
hash.put(current_element, diff);
end if
if(hash.contains(diff))
the two numbers are current_element, diff
end if
end for

Here is the tested java code for the same:

public class IntegerArrayTest {
public static final int[] int_array =
{1,2,3,4,5,6,7,8,9,10};
public static java.util.Hashtable intHash =
new java.util.Hashtable();
public static String getResult(int x_number) {
for (int i=0;i int current_element = int_array[i];
int diff = 0;
if( x_number > current_element)
diff = x_number - current_element;
else
diff = current_element - x_number;
if(!intHash.containsKey(current_element)) {
//just add it in
intHash.put(current_element, diff);
}
if(intHash.containsKey(diff)){
//out quest stops here
return new StringBuffer("The numbers are ").
append(current_element).append(" and ").
append(diff).toString();
}
}
return "Did not find mathing number";
}
public static void main(String args[]) {
String x_number = args[0];
String result =
IntegerArrayTest.getResult(
Integer.parseInt(x_number));
System.out.println(result);
}
}

There you go, thats my take on that algorithm. Let me know how it can be enhanced.