Apple Interview Question for Software Engineer / Developers


Team: Sales
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
6
of 8 vote

For maintaining the nearest empty slot use min heap. Put the slot when someone leaves and remove and give when someone arrives. For maintaining filled slots use hash map. Remove when someone leaves and add when someone arrives.

- nitiraj February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Amazing I had the same answer!

- byteattack June 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for maintaining filled slots you can also use Set. Add and remove to and from the Set. All this happens in constant time.

- romil.bm April 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since we have fixed number of slots, why cant we use a simple stack where slots are pushed farthest to nearest. Nearest spot will be on the top. Keep poping the slots as car arrives. Push the slot when a car leaves.

- looper September 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since the parking lot size is fixed, why cant we use a simple stack? You can push the slots farthest to nearest. Nearest one will be at the top. Keep poping the slots as the cars arrive. Push the slot back in when the car leaves.

- looper September 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

simple stack won't work unless cars leave in farthest to nearest order. since a stack only adds to back, if a car that is nearer to entrance leaves before a car that is farther, the further will be returned first. a priority queue which assigns priority according to distance from entrance is better

- lamron January 02, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can just use a Matrix of Bitarray to represent the parking lot, and use a Queue to update empty one. and when some new vehical arrives you can take the entry from queue and return it.

- kirankumarcelestial February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Adding OO Code using PriorityQueue. You can enhance this to use a blocking queue if multiple entrances/exits exist.

public class ParkingLot {
	
	private static final int maxFloors = 5;
	private static final int maxSlotsPerFloor = 10;
	
	private PriorityQueue<ParkingSpace> pq = new PriorityQueue<ParkingLot.ParkingSpace>((maxFloors * maxSlotsPerFloor), 
			new Comparator<ParkingSpace>() {
				@Override
				public int compare(ParkingSpace o1, ParkingSpace o2) {
					
					if(o1.getFloor() == o2.getFloor()) {
						return o1.getSlot() - o2.getSlot();
					}
					else 
						return o1.getFloor() - o2.getFloor();					
				}				
			}
	);
	
	public ParkingSpace park(){
		ParkingSpace ps = getNextAvailable();
		if(ps == null) {
			throw new IllegalStateException("Parking Lot is Full.");
		}
		pq.remove(ps);
		return ps;
	}
	
	public void unpark(int floor, int slot) {
		ParkingSpace ps = new ParkingSpace(floor, slot);
		if(!pq.contains(ps)) {
			pq.add(ps);
		}
		else {
			throw new IllegalStateException("Invalid Parking Lot.");
		}
		
	}
	
	public void addParkingSpace (int floor, int slot) {
		ParkingSpace ps = new ParkingSpace(floor, slot);
		pq.add(ps);
	}
	
	public ParkingSpace getNextAvailable(){
		if(pq.size() > 0)
		{
			return pq.peek();	
		}
		return null;
	}
	
	public static void main(String[] args) {

		ParkingLot pl = new ParkingLot();
		
		pl.addParkingSpace(1, 1);
		pl.addParkingSpace(2, 1);
		pl.addParkingSpace(3, 1);
		pl.addParkingSpace(1, 2);
		pl.addParkingSpace(2, 2);
		pl.addParkingSpace(3, 2);
		
		ParkingSpace n = pl.getNextAvailable();
		System.out.println("Parked at Floor: " + n.getFloor() + ", Slot: " + n.getSlot());
		pl.unpark(2, n.getSlot());
		
	}
	
	class ParkingSpace {
		private int floor;
		private int slot;
		
		public int getFloor() {
			return floor;
		}
		
		public int getSlot() {
			return slot;
		}
		
		public ParkingSpace(int floor, int slot) {
			if(floor > maxFloors || slot > maxSlotsPerFloor) {
				throw new IllegalArgumentException("Capacity is 5 floors and 10 slots per floor.");
			}
			
			this.floor = floor;
			this.slot = slot;
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof ParkingSpace) {
				ParkingSpace ps = (ParkingSpace) obj;
				return (this.getFloor() == ps.getFloor() &&
						this.getSlot() == ps.getSlot());
			}
			return false;
		}
	}
}

- balaji2104 May 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

since slots have numbers associated as the closer slot w/ a lower number, sorted tree(heap) to provide the closest slot. an object array represents the occupation, which gives you a quick look up time for availability . a list structure that contains slot object that mappes at the array object will give you all the occupied slots.

when one slot is freed up, throw it back to the heap and heapify

- dc May 12, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More