Interview Question and Answer: Elevator Algorithm

Posted on Updated on

Image

One of the interview questions asked is “Elevator Algorithm”. As you think this problem seems easy, but you might not be easily think of the algorithm in a fast manner. I have written a simple version of it and I think this would be enough for an interview.

ElevatorCabin Class

This class is the core class that nearly all of the logic runs, so it will be enough for you to view this thorough.

package org.tuna.elevator;

import java.util.TreeSet;

public class ElevatorCabin {
	
	public int currentFloor = 0;
	public UpDownEnum direction = UpDownEnum.UP;
	public TreeSet<Integer> callingFloorList  = new TreeSet<Integer>();
	
	public void add(Integer floor){
		callingFloorList.add(floor);
	}
	
	public Integer gotoNext(){
		Integer nextFloor = currentFloor;
		switch(direction){
		case UP:
			for (Integer floor : callingFloorList) {
				if(floor > nextFloor){
					nextFloor = floor;
					direction = UpDownEnum.UP;
					break;
				}
			}
			
			if(nextFloor == currentFloor){
				for (Integer floor : callingFloorList.descendingSet()) {
					if(floor < nextFloor){
						nextFloor = floor;
						direction = UpDownEnum.DOWN;
						break;
					}
				}
			}
			break;
		case DOWN:
			for (Integer floor : callingFloorList.descendingSet()) {
				if(floor < nextFloor){
					nextFloor = floor;
					direction = UpDownEnum.DOWN;
					break;
				}
			}
			
			if(nextFloor == currentFloor){
				for (Integer floor : callingFloorList) {
					if(floor > nextFloor){
						nextFloor = floor;
						direction = UpDownEnum.UP;
						break;
					}
				}
			}
		}
		callingFloorList.remove(nextFloor);
		currentFloor = nextFloor;
		return nextFloor;
	}
}

ElevatorEngine Class

This class is the engine that will simulate the work of an elevator using the thread mechanism.

package org.tuna.elevator;

public class ElevatorEngine extends Thread{
	
	private ElevatorCabin cabin = new ElevatorCabin();
	private volatile boolean stopFlag = false;
	
	public void startEngine(){
		stopFlag = false;
		this.start();
	}
	
	public void stopEngine(){
		stopFlag = true;
	}
	
	public void run(){
		while(true){
			if(stopFlag){
				if(cabin.callingFloorList.isEmpty()){					
					break;
				}
			}
			Integer next = cabin.gotoNext();
			System.out.println("Next floor is "+next);
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void pressButton(int floor){
		System.out.println("Pressed "+floor);
		cabin.add(floor);
	}
}

ElevatorEngineDriver Class

The driver classs to test the algorithm.

package org.tuna.elevator;

public class ElevetorEngineDriver {
	public static void main(String[] args) {
		ElevatorEngine engine = new ElevatorEngine();
		engine.startEngine();
		
		engine.pressButton(1);
		sleep();
		engine.pressButton(5);
		engine.pressButton(6);
		engine.pressButton(1);
		engine.pressButton(4);
		sleep();sleep();sleep();
		engine.pressButton(2);
		engine.pressButton(9);
		engine.pressButton(1);
		sleep();
		engine.stopEngine();
		try {
			engine.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void sleep(){
		try {
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

Advertisements

One thought on “Interview Question and Answer: Elevator Algorithm

    iamsocurious said:
    June 6, 2016 at 5:48 pm

    Reblogged this on Iamsocurious.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s