Code WorkOut of the Day -- Daily exercises for programmers looking for jobs, or just to keep sharp with skills and have fun.
I give talks, like this: https://youtu.be/NpvTE7GlXSM for people looking for jobs, or groups of programmers preparing for M&A tech HR due diligence.
Follow us on twitter: @codewod

How to

When solving these problems, learning by doing is much better than learning by reading. I encourage to you read only as far in the solution as you need, then trying to solve the problem. If you get stuck, try reading a little further. And of course, let me know if you find a better solution!

Wednesday, February 22, 2012

Elevator Algorithm

Problem: Describe an algorithm that you could use if
programming an elevator.

Solution: No custom elevator knowledge is assumed
here. Let’s start by drawing a picture
to allow us to make sure that we understand the problem.

Pic 1 is a simple picture of an elevator and some floors it
can stop on.

Let’s start by talking about the characteristics of the
problem, which we will have to optimize:

There are
a certain number of floors on which the elevator can stop

It takes a certain amount of time for the elevator
doors to open and close

It takes time for the elevator to accelerate,
stop and has a max speed

People can signal the elevator and indicate
whether they are going up or down

The elevator has a maximum capacity

Now, lets’ talk about some of the basic elements of an
algorithm for an elevator:

Elevators
should minimize waiting times for people who are signaling the elevator

Once an elevator starts going up and someone
indicates a higher floor on it, the evator should not go down

Same as 2, but for down

Elevators should minimize transit times for
people in the elevator

Well, already, this is looking much more complicated. We have multiple characteristics and
rules. Let’s now put this together, and
start talking about what we know about elevators:

When
someone is in the elevator, it is going up or down.

It will go up and stop at every floor where
either: 1) someone in the elevator has selected, or 2) someone outside the
elevator has pressed the up button

It will go down and stop at every floor where:
1) someone in the elevator has selected, or 2) someone outside the elevator has
pressed the button

It will always stop at the bottom floor when
going down if someone has pressed the up button

It will always stop at the top floor when going
up if someone has pressed the down button

These are the basics.
Now, let’s try an example. Assume
that someone walks into a building and presses the up button on the bottom
floor – what happens?

Well, assuming no one is in the elevator, it will come down
from the floor it is on. So, the person
will have to wait while the elevator comes down.

This example highlights another issue. Where does the elevator wait when it is not
in use?

One first thought would be to wait in the middle. This would seem to optimize between people
going up and people going down. We can
probably assume too that there are relatively few trips between floors – though
even this may not be true if for example a company has offices on more than one
floor.

But, let’s keep it simple.
But, think about someone coming in to go up. When do people do this? And, when do people go down primarily?

This would depend a little on the type of building. In an office building, people will often come
in in the morning and leave in the evening.
In an apartment building, it would be the opposite.

One optimization could be to move the elevator closer to
where people are expected at different times of day. This could reduce wait times dramatically.

We have most of what we need to write out an algorithm. Let’s do it now:

First, let’s bring in all the rules that are expected:

1 When someone is in the elevator, it is going up
or down.

a.
It will go up and stop at every floor where
either: 1) someone in the elevator has selected, or 2) someone outside the
elevator has pressed the up button

b.
It will go down and stop at every floor where:
1) someone in the elevator has selected, or 2) someone outside the elevator has
pressed the button

2 It will always stop at the bottom floor when
going down if someone has pressed the up button

3 It will always stop at the top floor when going
up if someone has pressed the down button

And, let’s add, assuming we have an office building:

The
elevator will wait at the bottom floor when not in use between 7am-10am.

The elevator will wait at the top floor when not
in use between 3pm – 7pm.

At other times, the elevator will wait in the
middle floor

Well, we have a good algorithm here.

Most important in this problem, is to 1) not ignore the
obvious such as the rules of how an elevator works and what it is trying to
optimize, and 2) always seek to ‘stretch’ your answer. In this case, we stretched with an
optimization problem.

1 comment:

From my friend Steve: Should I assume the floors are in order? The floors numbers in US buildings look like alphabet soup. The newest technology has users punch in their floor number in the lobby and the cabs have no buttons. Also, how many local and express cabs are in the bank, and are we optimizing for dispatch time or trip time? Are there up peak and down peak periods? Do we trigger those periods by car load factor or clock?

30 minutes, once a week, free

To help the community, I do one 30 minute interview every week for free for candidates (virtual, over some online coding tools). Email me if you're interested. It's good practice for the kinds of questions you'll get, and I'll give honest feedback as to your skills, likelihood of getting a job, and tips for improving/practicing for future interviews.

I especially like to work with minority candidates, and those looking to make career changes, so please shoot me an email, provide some background, and we'll set up a time.

noah@codewod.com

Also, if you find alternative or better answers to the questions, please let me know and I'll post/credit!

About Me

About me: I'm currently the founder of Workhood (www.workhood.com) and have spent over 10 years in the technology industry. Prior to Workhood, I was the co-founder of SocialShield, acquired by Avira (A/V security company with over 100m users), where I was subsequently the VP Technology/Operations. I worked previously in McKinsey's technology practice in London and New York and also worked at several start-ups as an engineer and product manager, founding my first venture backed company when I was 22 -- and have raised over $35M in my career in financing. My focus is always on delivering great products quickly and I am a huge proponent of agile programming/product development and the lean start-up techniques. I'm a Stanford CS grad, always interested in coding problems -- co-author of the best-selling book: Programming Interviews Exposed: Secrets to Landing Your Next Job. I also went to HBS and am an avid college football and soccer fan.

I blog about coding problems at: www.codewod.com and about start-ups at www.noahkindler.com. I live in the Bay Area and can often be found at Crossfit or on the slopes when I manage to sneak out.