由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - cc150 - design OOP parking lot problem
相关主题
Careercup 设计题Parking Lot讨论前段时间整理的随机算法
OO design的核心是不是忽悠?请教一道题目
amazon电面 + facebook 电面Maximum Sum of Increasing Sequence
去很冷的地方interview,穿什么?这是什么数据结构?
两道经典design问题求助求救: 打印binary tree
面试题:如何设计一个停车场。问个题
请问OPT期间,part time 的job,可以么?Google 电话面试被拒
问道题Amazon的Fibonacci题
相关话题的讨论汇总
话题: spot话题: type话题: vehicle话题: int话题: row
进入JobHunting版参与讨论
1 (共1页)
h**o
发帖数: 548
1
书上完整答案在哪儿? 书上说 in the downloadable code attachment. 不知道指什
么。
另外,我自己写的和它很不一样。有些functions 我不清楚到底写在那个类合适。 例
如:
park(), unpark() findAvaiableSlots()写在 class vehicle, 还是 class
ParkingLot 还是 ParkingSlot 好。
c********p
发帖数: 1969
2
/。。。。。。。。我正想问这个,你就问了。。。
s*****p
发帖数: 26
p*****2
发帖数: 21240
4
我觉得书上给的solution也不匝地
h**o
发帖数: 548
5
谢谢 sunsnap. cc150 的 答案 挺绕的。
二爷有没有比较好的思路?
你用什么数据结构存parking spot?
目前我用一维数组: parking_spot = new array [capacity];

【在 p*****2 的大作中提到】
: 我觉得书上给的solution也不匝地
c********p
发帖数: 1969
6
对我也觉得这个答案不怎么好。你post出来你的答案给我们看看呗!

【在 h**o 的大作中提到】
: 谢谢 sunsnap. cc150 的 答案 挺绕的。
: 二爷有没有比较好的思路?
: 你用什么数据结构存parking spot?
: 目前我用一维数组: parking_spot = new array [capacity];

u*****o
发帖数: 1224
7
http://stackoverflow.com/questions/764933/amazon-interview-ques
这里面有三个人点赞的那个回复我觉得不错啊,挺清楚的。。
h**o
发帖数: 548
8
int test_parking_lots(){
ParkingLots p(3,40,50);//floor, row, column
Car car1 = Car();
Motor motor1 = Motor();
Bus bus1 = Bus();
int car1_id, motor1_id, bus1_id;
bool res;
res = p.park(car1, car1_id);
res = p.park(motor1, motor1_id);
res = p.park(bus1, bus1_id);
res = p.unpark(car1, car1_id);
return 0;
}
typedef enum VehicleType_e{
MOTOR_VEHICLE_TYPE = 0,
CAR_VEHICLE_TYPE,
BUS_VEHICLE_TYPE,
MAX_VEHICLE_TYPE_SIZE
} VehicleType;
typedef enum SpotType_e{
INVALID_SPOT_TYPE = -1,
COMPACT_SPOT_TYPE = 0,
LARGE_SPOT_TYPE,
MAX_SPOT_TYPE_SIZE
} SpotType;
typedef enum Needed_Spot_Num_e{
MOTOR_SPOT_NUM = 1,
CAR_SPOT_NUM = 1,
BUS_SPOT_NUM = 5
} Needed_Spot_Num;
class Vehicle{
public:
Vehicle(int num_spots):num_of_spots(num_spots){}
virtual void fitted_spot_type() = 0;
int num_of_spots;//note member variable of base must be assigned at base
class, not subclass.
bool canPark(SpotType spot_type);//can the vehicle park at this spot_
type
vector spot_type_list; //supported spot types
};
bool Vehicle::canPark(SpotType spot_type){
vector::iterator itt;
for (itt = spot_type_list.begin(); itt != spot_type_list.end();++itt
){
if (spot_type == *itt) return true;
}
return false;
}
class Car: public Vehicle{
public:
Car():Vehicle(CAR_SPOT_NUM){fitted_spot_type();} //note how num_of_spots
is assigned;
void fitted_spot_type(){spot_type_list.push_back(LARGE_SPOT_TYPE);}
};
class Motor: public Vehicle{
public:
Motor():Vehicle(MOTOR_SPOT_NUM){fitted_spot_type();}
void fitted_spot_type(){spot_type_list.push_back(COMPACT_SPOT_TYPE);spot
_type_list.push_back(LARGE_SPOT_TYPE);}
};
class Bus: public Vehicle{
public:
Bus():Vehicle(BUS_SPOT_NUM){fitted_spot_type();}
void fitted_spot_type(){spot_type_list.push_back(LARGE_SPOT_TYPE);}
};
class ParkingSpot{
public:
SpotType type;
int floor;
int row;
int column;
bool is_avaiable;
};
class ParkingLots{
public:
ParkingLots(int floor_size0, int row_size0, int col_size0):
floor_size(floor_size0), row_size(row_size0), col_size(col_size0){init
();}
~ParkingLots(){delete [] parking_spot;}
//vector * parking_spot;
ParkingSpot* parking_spot;
bool park(Vehicle& v, int& id);
bool unpark(Vehicle& v, int id);
bool have_enough_spots(Vehicle& v, int id);
private:
int floor_size, col_size, row_size, capacity;
void init();
};
void ParkingLots::init(){
assert(floor_size>0 && row_size>0 && col_size>0);
capacity = floor_size * row_size * col_size;
//parking_spot = new vector [capacity];
parking_spot = new ParkingSpot[capacity];
ParkingSpot* a = NULL;
int capacity_of_one_floor = col_size * row_size;
for (int i = 0; i < capacity; i++){
a = parking_spot+i;

if (i < 20) a->type = COMPACT_SPOT_TYPE;//assume
else a->type = LARGE_SPOT_TYPE;
a->is_avaiable = true;
a->row = i/col_size;
a->column = i % col_size;
a->floor = i /capacity_of_one_floor;
}
}
bool ParkingLots::park(Vehicle& v, int& id){
ParkingSpot* a = NULL;
for (int i = 0; i < capacity; i++){
a = parking_spot+i;
if (a->is_avaiable && have_enough_spots(v, i)){
id = i;
for(int j = 0; j < v.num_of_spots; j++){
a->is_avaiable = false;
}
return true;
}
}
return false;
}
bool ParkingLots::unpark(Vehicle& v, int id){
int row = id/col_size;
int col = id % col_size;
int floor = id /(col_size * row_size);
assert (floor < floor_size && row < row_size && col< col_size);
ParkingSpot* a = NULL;
for(int j = 0; j < v.num_of_spots; j++){
a = parking_spot+id + j;
if (a->is_avaiable) return false;//no vehicle here at all.
a->is_avaiable = true;
}
return true;
}
//check whether vehicle v can park starting at spot parking_spot[id]
bool ParkingLots::have_enough_spots(Vehicle& v,int id){
ParkingSpot* a = NULL;
int row, floor;

bool res = false;
for (int i = 0; i < v.num_of_spots; i++){
a = parking_spot+id + i;
if (i == 0) {row = a->row;floor = a->floor;}
if (a->floor != floor || a->row != row) return false; //no space in
the same floor and row
if (!v.canPark(a->type)) return false;//spot is not available
}
return true;
}

【在 c********p 的大作中提到】
: 对我也觉得这个答案不怎么好。你post出来你的答案给我们看看呗!
h**o
发帖数: 548
9
xiexie.是挺简单的。

【在 u*****o 的大作中提到】
: http://stackoverflow.com/questions/764933/amazon-interview-ques
: 这里面有三个人点赞的那个回复我觉得不错啊,挺清楚的。。

h**o
发帖数: 548
10
parking lot 这道题想考我们什么?
chat server 这道题想考我们什么?

【在 h**o 的大作中提到】
: xiexie.是挺简单的。
h****p
发帖数: 87
11
mark
u*****o
发帖数: 1224
12
chat server 那题有sample code可以参考吗?
我还知道一个DESIGN ELEVATOR的。。
这三题应该算是DESIGN里的三家马车了吧,时不时出来恶心一下大家。。

【在 h**o 的大作中提到】
: parking lot 这道题想考我们什么?
: chat server 这道题想考我们什么?

h**o
发帖数: 548
13
+hash?, not sure poker card

【在 u*****o 的大作中提到】
: chat server 那题有sample code可以参考吗?
: 我还知道一个DESIGN ELEVATOR的。。
: 这三题应该算是DESIGN里的三家马车了吧,时不时出来恶心一下大家。。

h**o
发帖数: 548
14
不过这个code把vehicle简化了,parking rule 也没有。否则没怎么短。

【在 u*****o 的大作中提到】
: http://stackoverflow.com/questions/764933/amazon-interview-ques
: 这里面有三个人点赞的那个回复我觉得不错啊,挺清楚的。。

1 (共1页)
进入JobHunting版参与讨论
相关主题
Amazon的Fibonacci题两道经典design问题求助
攒RP, 发Amazon第二轮电话面经面试题:如何设计一个停车场。
问Thomson Reuters两道算法题请问OPT期间,part time 的job,可以么?
火柴问题问道题
Careercup 设计题Parking Lot讨论前段时间整理的随机算法
OO design的核心是不是忽悠?请教一道题目
amazon电面 + facebook 电面Maximum Sum of Increasing Sequence
去很冷的地方interview,穿什么?这是什么数据结构?
相关话题的讨论汇总
话题: spot话题: type话题: vehicle话题: int话题: row