一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 2961|回复: 4
收起左侧

电梯设计题目总结

[复制链接] |试试Instant~ |关注本帖
chenwoo 发表于 2015-8-5 00:57:59 | 显示全部楼层 |阅读模式

2015(7-9月) 码农类 硕士 全职@Amazon - 网上海投 - Onsite |Passfresh grad应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
楼主前段时间Amazon onsite,遇到了Amazon经典的电梯设计题目,下面是我对这个题目的总结,也是答案不是最好的,欢迎大家来补充。

. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

我感觉对于OOP设计问题,关键要和面试官进行讨论,弄清情景situation ,who will use it,在这个situation都是有哪些对象,他们在干什么,他们之间是什么关系,然后一个对象一个对象的去分析这个对象应有的属性,和行为。多和面试官讨论,越细致越好!如果可以使用什么单例,或者factory method 来设计的话,multi threading, 就尽量的添加上这些东西。

elevator:
First ask the interviewer what kind of elevator?  there is only one elevator serving that building or multiple elevators serving the building simultaneously?
this situation is that: there is one elevator serving the building.  there are many floors in the buliding. Maybe there are some users in different floor pressing the button simultaneously. This results in some requests to RequestProcessCenter for processing. The  RequestProcessCenter figure out the first request that need to be processed in such an algorithm that the distance between target floor and current floor is shortest.
First describe the whole situation. and check it with your interviewer;
Second sketch out the main classes and methods on the whiteboard;
So we need the following classes:
public class User {
private name;
public pressButton(int toFloor) {
    Request req = new Request( toFloor);
    RequestProcessCenter  center = RequestProcessCenter.getInstance();
    center.addRequest(req);
}
}
public class Request {
    private int toFloor;
    public Request(int _toFloor) {
        toFloor = _toFloor;
}
public getToFloor() {
    return toFloor;
}
}
public class Elevator {
    public static Elevator instance = null;
    private int currentFloor;
    public static Elevator( ) {
        if (instance == null) {  // late loading and eager loading
                    // connection pool
            synchronized (Elevator.class) {
                instance = new Elevator();
}
}
return instance;
}
public getInstance() {
    if (instance == null) {
            synchronized (SingletonDemo.class) {
                instance = new Elevator();
}
}
return instance;
}
public getCurrentFloor() {
    return currentFloor;
}
public moveToTargetFloor(int toFloor) {
    currentFloor = toFloor;
}
public void moveUp();
public void moveDown();
}
public RequestProcessCenter implements runnable {
    public LinkedList<Request> queue;
public RequestProcessCenter( ) {
        queue = new LinkedList<Request>( );
}
public void run() {
        while ( true ) {
            processRequest( )
}
}
public void addRequest(Request request) {
    queue.add(request);
}
public void removeRequest(Request request) {
    queue.remove(request);
}
public Request getNextRequest( ) {
    Request shortestReq = null;
    int shortest = Integer.MAX_VALUE;
    int curFloor = Elevator.getInstance( ).getCurrentFloor( );
    for (Request item : queue) {
        int distance = Math.abs(curFloor - item.getToFloor( ) );
        if (distance < shortest) {
            shortest = distance;
            shortestReq = item;
}
}
return shortestReq;
}
public void processRequest( ) {
    Request req = getNextRequest( );
if (req != null) {
        int toFloor = req.getToFloor( );
        Elevator.getInstance.moveToTargetFloor( toFloor);
        queue.remove(req);
}
   
}
}
. 1point 3acres 璁哄潧


评分

2

查看全部评分

夏末微凉 发表于 2015-10-13 03:45:30 | 显示全部楼层
楼主你好,我想请问下这一步:
public getInstance() {
    if (instance == null) {
          synchronized (SingletonDemo.class) {
                instance = new Elevator();
}
这里面的singletonDemo.class是什么意思呢? 为什么要单独定义一个getInstance() method ?
回复 支持 反对

使用道具 举报

xiangxiang 发表于 2015-10-15 00:28:52 | 显示全部楼层
夏末微凉 发表于 2015-10-13 03:45. 1point3acres.com/bbs
楼主你好,我想请问下这一步:
public getInstance() {
    if (instance == null) {

singleton design pattern 吧
回复 支持 反对

使用道具 举报

陈润鹏 发表于 2016-6-21 04:05:05 | 显示全部楼层
LZ的电梯好像缺少一个方向 一旦有请求 离得远的 就几乎别想坐上电梯了
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-7 16:49

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表