Posted by Marta on January 26, 2021 Viewed 12587 times

You know practice is important to **get better at programming**, but what should you program? A great practice exercise is design and implement an elevator system, which is actually a real java coding interview question.

This article will show **how to design an elevator system in Java**, which is the sort of exercise that you will face during the screening phase of an interview process. Some of the coding questions can be challenging. That’s why is important to practice and get comfortable at solving these problems.

I will introduce the **design elevator system problem**. Then one possible approach to solve the problem, and the Java solution. This exercise is an excellent practice for an interview and also quite fun to solve.

Let’s get started!

Here is the exercise:

Our company has to** improve the old elevator system**. When a person presses the elevator button on any floor, the elevator system should find the elevator closest to the floor, which will make the system more energy efficient.

Our elevator system will create a **building elevator area**, which represents the elevator’s doors per floor. This area is abstracted as a grid:

When someone presses an elevator button on any floor, the system must determine the **minimum distance** required for one of the elevators to reach the requested floor.

The algorithm should return an integer representing the minimum distance to get an elevator to the requested floor. **See below examples of the inputs the algorithm will receive and the expected outputs**. For instance, in the example above, if someone calls the elevator on the second floor, the outcome will be 2.

So far, we have seen the problem; let’s now analyze the inputs that the algorithm will receive and the expected output in different scenarios.

**Case #1 – Input**

// Input int[][] grid = {{0,1,0,1},{0,0,0,0}, {0,0,0,0},{1,0,1,0}}; int floor = 2; // Floor where a person pressed the button

**Output:**

1

The

represents the elevator area, where the first row refers to the ground floor, the second row represents the first floor, and so on. The floor variable refers to the **grid**

where the button was pressed.**floor**

Let’s see a few more examples:

// Inputs int[][] grid = {{1,1,1,1},{0,0,0,0}, {0,0,0,0},{0,0,0,0}}; int floor = 3; //Output 3

// Inputs - Same grid as previous int[][] grid = {{1,1,1,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; int floor = 0; //Output 0

// Inputs int[][] grid = {{1,1,0,0},{0,0,0,0},{0,0,1,0},{0,0,0,0},{0,0,0,1}}; int floor = 3; // Outputs 1

// Inputs int[][] grid ={{1,1,0,1,1},{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}; int floor = 4; // Outputs 3

And here is the class that you are expected to complete during the java programming interview. The **time to complete assignments similar to this one is about 45 minutes to 1 hour**.

public class ElevatorSystem { public int findMinimumDistance(int floor, int[][] area){ return -1; } }

I will advise you to try to come up with a solution yourself before checking the answer.

Let’s see the solution. You could first** solve the most straightforward case scenario**, pressing a button on a floor where there is an elevator. In this case, the output will be 0. For this case, all you need to do is writing a method that checks if an array contains the number 1.

public boolean isElevatorOnFloor(int[] elevatorRow){ return Arrays.binarySearch(elevatorRow,1)>-1; }

Now, let’s see a **more complicated case**. What if there is no elevator on this floor? You will need to check the row above and the row below, making sure the rows are not out of the grid. In case there is no elevator in those rows, the algorithm will have to continue to the next rows.

See below how you can **check if the row is out of the grid**. This code will ensure the variable `row`

is equal or greater than 0 and less than the grid size.

public boolean isRowWithinGridLimit(int numRows, int row){ return row >- 1 && row < numRows; }

And once we have completed those helper function, you have the necessary operations to write the final solution:

public int findMinimumDistance(int floor, int[][] area){ if(isElevatorOnFloor(area[floor])){ return 0; } int i = 1; int rowAbove = floor+i; int rowBelow = floor-i; while(isRowWithinGridLimit(area.length, rowAbove) || isRowWithinGridLimit(area.length, rowBelow)){ if(isRowWithinGridLimit(area.length, rowAbove)){ if (isElevatorOnFloor(area[rowAbove])){ return i; } } if(isRowWithinGridLimit(area.length, rowBelow)){ if(isElevatorOnFloor(area[rowBelow])){ return i; } } i++; rowAbove = floor+i; rowBelow = floor-i; } return -1; }

In case there is an elevator on the floor where the person presses the button, no elevator needs to move so that the algorithm will return 0. Otherwise, it will start iterating through the rows to check if there is an elevator. It will keep running the loop until the `rowAbove`

and `rowBelow`

indexes reach the grid limit.

In case you would like to **check that your solution works**, here is a sample unit test. Adding a unit test is a good practice since it allows you to make sure your code works, and more importantly, do so automatically. See the JUnit test below:

import org.junit.Assert; import org.junit.Test; public class ElevatorSystemTest { @Test public void testCase1(){ int[][] grid = {{0,1,0,1},{0,0,0,0}, {0,0,0,0},{1,0,1,0}}; Assert.assertEquals(1, new ElevatorSystem().findMinimumDistance(2,grid)); } @Test public void testCase2(){ int[][] grid = {{1,1,1,1},{0,0,0,0}, {0,0,0,0},{0,0,0,0}}; Assert.assertEquals(3, new ElevatorSystem().findMinimumDistance(3,grid)); Assert.assertEquals(0, new ElevatorSystem().findMinimumDistance(0,grid)); } @Test public void testCase3(){ int[][] grid = {{1,1,0,0},{0,0,0,0},{0,0,1,0},{0,0,0,0},{0,0,0,1}}; Assert.assertEquals(1, new ElevatorSystem().findMinimumDistance(3,grid)); Assert.assertEquals(0, new ElevatorSystem().findMinimumDistance(2,grid)); } @Test public void testCase4(){ int[][] grid ={{1,1,0,1,1},{0,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}; Assert.assertEquals(3,new ElevatorSystem().findMinimumDistance(4,grid)); } }

To summarize, we have seen how to design and implement an elevator system from scratch. This java coding interview question will help you preparing for programming interviews. Plus, we have seen a full detailed explanation of how to solve the problem.

I hope you enjoy the article and find it helpful. Thank you so much for reading and supporting this blog!

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

Unique lay-out and teaching programming style helping new concepts stick in your memory

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Excellent read for anyone who already know how to program and want to learn Best Practices

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java