Friday, July 26, 2013

Careercup 设计题Parking Lot讨论

发信人: smilenceyu (smilence), 信区: JobHunting
标  题: Careercup 设计题Parking Lot讨论
关键字: 面试题,careercup
发信站: BBS 未名空间站 (Fri Jul 26 00:08:19 2013, 美东)

一直对设计题比较头疼,虽知道这个没什么标准答案,但是跟答案元素差的比较多的话
,也不知道自己做到什么程度。如果强迫自己“背”答案,又会发现不符合自己的思路
,比如这道题目的答案中vehicle和spot都有parkVehicle这样的函数,让人很费解。

题目就一句话:Design a parking lot using object-oriented principles.

所以想把自己第一反应写出来的设计拿出来请教下。有些地方省略了,反正
implementation也不重要。

class ParkingLot{

    private:
        level* lvl;
        int max_level;
        static ParkingLot* pInstance;
       
    public:

        static ParkingLot* getInstance();

        pair<int,int> parkVehicle( Vehicle* v){
            //iterate all levels, invoke parkFreeSpot(v);
           
            int index;
            for( int j = 0 ; j < max_level ; j++){
           
                index = lvl[j].parkFreeSpot(v);
                if( index != -1){
                    return pair<int,int>(j,index) ;
                }
            }
            return -1;
        }
       
}

class level{
    private:
        Spot* spots;
        int max_num;
        int remain;
        int freeIndex;
       
    public:
        int parkFreeSpot( Vehicle* v){
            int n = v->numSpots();
            int index = findFreeSpots( n);//find continuous n spots,return
the index;
           
            if(index = -1) return -1;
            else
                 //park the vehicle at spot(s);
                v->parkVehicle( spots+index);

            return index;
           
        }
       
        int findFreeSpots( int n){
            //find continuous n spots,return the index;
            //if not found, return -1;
        }
}




--

※ 修改:·smilenceyu 於 Jul 26 00:17:46 2013 修改本文·[FROM: 71.]
※ 来源:·WWW 未名空间站 海外: mitbbs.com 中国: mitbbs.cn·[FROM: 71.]

http://www.mitbbs.com/article_t/JobHunting/32492327.html

No comments:

Post a Comment