传说中的谷歌招聘委员会成员之一,从幕后走出来,教你学系统设计!


一亩三分地论坛

 找回密码
 获取更多干活,快来注册
Babeltime游戏工作室招工程师、美术和策划
Tubi TV招安卓、前端和机器学习工程师
把贵司招聘信息放这里
查看: 7137|回复: 4
收起左侧

电梯设计题目总结

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

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

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

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);
}
   
}
}



评分

2

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

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

使用道具 举报

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

singleton design pattern 吧
回复 支持 反对

使用道具 举报

本版积分规则

关闭

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

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-9-21 15:16

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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