由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 谈谈刚面的一个design题
相关主题
uber 的一个面试题大G面试 on-site报销饭钱这么抠门?
Amamon onsite 面经南湾区San Jose公司需要招聘一名办公室文员
有人来讨论下OOD的题目通常怎么答吗?南湾区San Jose公司需要招聘一名办公室文员
几个OOP的面试题设计card deck问题,还有shuffle function,大家要搞清楚呀
Amazon Interview求介绍设计parking lot的文档
OOD问题总结Amazon 两轮电话面经 及 design问题请教
帮忙看个南加州小公司会计offer请问设计一个虚拟的动物园,用什么设计模式好啊?
Microsoft Beijing Recruiting (转载)one C/C++ question
相关话题的讨论汇总
话题: beverage话题: vending话题: int话题: unsigned话题: class
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理?
g**e
发帖数: 6127
2
能把m_beverages改成Map吗

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

w****x
发帖数: 2483
3

就是你们家的题啊。 有的时候感觉就是要知道子类类型,就是一个trade off.
当然可以改成map啊

【在 g**e 的大作中提到】
: 能把m_beverages改成Map吗
n******n
发帖数: 567
4
factory??
g**e
发帖数: 6127
5
Map> m_beverage;
boolean getBeverage(Type ntype) {
if (m_beverage.contains(ntype)
&& m_beverage.get(ntype).size()>0) {
m_beverage.get(ntype).remove(0);
return true;
} else {
return false;
}
}
public enum Type {
COKE, PEPSI, ...
}
为什么不能知道子类型?买东西的时候总要选一个,把类型enum pass进来就行了

【在 w****x 的大作中提到】
:
: 就是你们家的题啊。 有的时候感觉就是要知道子类类型,就是一个trade off.
: 当然可以改成map啊

w****x
发帖数: 2483
6

你这和我的做法没太大区别啊, 同样是定义了类型.
根据下面的A家面试指南是一个大大的red flag.
https://sites.google.com/site/steveyegge2/five-essential-phone-screen-
questions
"They may produce an elaborate class tree for Animals, and then declare an
enum ("Lion = 1, Bear = 2", etc.) to represent the type of each animal."

【在 g**e 的大作中提到】
: Map> m_beverage;
: boolean getBeverage(Type ntype) {
: if (m_beverage.contains(ntype)
: && m_beverage.get(ntype).size()>0) {
: m_beverage.get(ntype).remove(0);
: return true;
: } else {
: return false;
: }
: }

g**e
发帖数: 6127
7
我以为你说的enum是给定的? 另外我觉得这题不需要sub class。实际上我的code也没
用subclass啊
用一堆subclass加enum是不好,跟这哥们说的一样

【在 w****x 的大作中提到】
:
: 你这和我的做法没太大区别啊, 同样是定义了类型.
: 根据下面的A家面试指南是一个大大的red flag.
: https://sites.google.com/site/steveyegge2/five-essential-phone-screen-
: questions
: "They may produce an elaborate class tree for Animals, and then declare an
: enum ("Lion = 1, Bear = 2", etc.) to represent the type of each animal."

w****x
发帖数: 2483
8

这题本来就是考OOD的,参考一下career cup 150上OOD的章节的答案都是过度设计,
本来出的面试题基本上都是简单的场景, 其实所有这种面试题类型都一个class搞定,
但是这么做的话又怕面试官说没有OO的思维, 分寸真难把握。

【在 g**e 的大作中提到】
: 我以为你说的enum是给定的? 另外我觉得这题不需要sub class。实际上我的code也没
: 用subclass啊
: 用一堆subclass加enum是不好,跟这哥们说的一样

t******k
发帖数: 64
9
Why they have this enum in the first place? They already made a design
decision for you. It is therefore a reasonable question to ask why they did
so. Likely they will say, this is in response to, for example, a customer
press a button. Then, in that case, let each subclass of Beverage respond to
a representation of that button.

【在 w****x 的大作中提到】
:
: 这题本来就是考OOD的,参考一下career cup 150上OOD的章节的答案都是过度设计,
: 本来出的面试题基本上都是简单的场景, 其实所有这种面试题类型都一个class搞定,
: 但是这么做的话又怕面试官说没有OO的思维, 分寸真难把握。

i***e
发帖数: 452
10
这种题真不知道如果设计可以过关了。。 A家贼喜欢问这类问题, 每次设计完了都感
觉不得要领。。 按说类型应该算是这个class的一个property才对, 那样的话就不应
subclass吧。
相关主题
OOD问题总结大G面试 on-site报销饭钱这么抠门?
帮忙看个南加州小公司会计offer南湾区San Jose公司需要招聘一名办公室文员
Microsoft Beijing Recruiting (转载)南湾区San Jose公司需要招聘一名办公室文员
进入JobHunting版参与讨论
S******1
发帖数: 269
11
当初挂在这道题目上了。。。
s***0
发帖数: 117
12
class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};

const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
// The Number of slots on a vending machine
// And the path to some xml file
class VendingRule
{
public:
// constructor from xml
VendingRule(boost::filesystem::path pathToXml, unsigned int slots);
// return the item goes in what slot given slot number.
// Instantiation of the item goes here.
VendingItem::Ptr getItem(unsigned int slot);
}
class VendingMachine
{
private:
// A vending machine can hold 10s of thousands of different items
// but the number of slot is fixed.
const unsigned int mSlots;

// how many items each slot holds
const unsigned int mSlotHold;
// A Vending machine holds the rule
mutable VendingRule mVendingRule;

// And here's a map of slot, item.

typedef map ItemMap;
ItemMap mItemMap;

public:
// Constructor, set config for this machine
VendingMachine(unsigned int slots,
unsigned int slotHold,
VendingRule vendingRule)
:mSlots(slots), mSlotHold(slotHold), mVendingRule(vendingRule)
{
fillItemMap();
}
void fillItemMap()
{
for (unsigned int i = 0; i < mSlots ++i)
{
for (unsigned int j = 0; j {
VendingItem::Ptr vendingItem;
// get the item from the rule
mItemMap[Point(i,j)] = new(mVendingRule.getItem(i));
}
}
}

unsigned int getItem(unsigned int slot)
{
// You can use a variety of logic here.
// to do the counting.
return priceInCents;
// return the price, throw exception if empty.
// have try-catch on the other side ready to handle.
}
// vending machine should be able to have new rules.
void changeRule(VendingRule vendingRule);
// you can do other stuff too
// e.g. if it's an online vending macnine
// it should be multi-threaded.

// it should talk to a bank to deposit it's money
// it should be able to request a refill of low items
// it may even have machine learning to request most
// popular items in most popular slots.
};
s*******n
发帖数: 344
13
典型的over design

★ 发自iPhone App: ChineseWeb 7.5

【在 s***0 的大作中提到】
: class VendingItem
: {
: public:
: typedef boost::shared_ptr Ptr;
: enum ItemName
: {
: CocaCola;
: Chips;
: Cookies;
: Condoms;

w****x
发帖数: 2483
14

要这么做的话获取price的时候就是要用switch case这样的,这样的情况肯定是典型的
polymorphism, 设计的少了怕体现不出OO思维(毕竟是考OOD的,就丢一个class感觉有
点冒险), 设计多了怕over design, 觉得没有完美纯粹的OOD。 感觉的确不好把握。

【在 i***e 的大作中提到】
: 这种题真不知道如果设计可以过关了。。 A家贼喜欢问这类问题, 每次设计完了都感
: 觉不得要领。。 按说类型应该算是这个class的一个property才对, 那样的话就不应
: subclass吧。

s***0
发帖数: 117
15
elaborate?

【在 s*******n 的大作中提到】
: 典型的over design
:
: ★ 发自iPhone App: ChineseWeb 7.5

g**e
发帖数: 6127
16
所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
invent and simplify, vocally self critical,bias of action, have backbone;
disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
但是你的设计要能体现出你具备上述几点素质

都感
不应

【在 w****x 的大作中提到】
:
: 要这么做的话获取price的时候就是要用switch case这样的,这样的情况肯定是典型的
: polymorphism, 设计的少了怕体现不出OO思维(毕竟是考OOD的,就丢一个class感觉有
: 点冒险), 设计多了怕over design, 觉得没有完美纯粹的OOD。 感觉的确不好把握。

i***e
发帖数: 452
17
也是, 感觉这方面的能力需要一定的经验才能收放自如了。。不是光靠做几道题能解
决的

;

【在 g**e 的大作中提到】
: 所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
: ,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
: invent and simplify, vocally self critical,bias of action, have backbone;
: disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
: 但是你的设计要能体现出你具备上述几点素质
:
: 都感
: 不应

h*******e
发帖数: 1377
18
up 一下。。正在做这类的题。。感觉几个经典的一个是parking lot再一个 vending
machine在一个就是 elevator了。。应该要不断和 考官沟通吧。
w****x
发帖数: 2483
19

;
恩, 学习了, 早知道就好了

【在 g**e 的大作中提到】
: 所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
: ,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
: invent and simplify, vocally self critical,bias of action, have backbone;
: disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
: 但是你的设计要能体现出你具备上述几点素质
:
: 都感
: 不应

d****o
发帖数: 1055
20
class vendingMachine{
private:
hash_map > m;
public:
bool getBeverage(int type){
if(m.find(type)==m.end()){
return false;
}
vector& cur = m[type];
if(cur.size()==0){
return false;
}
cur.pop_back();
return true;
}
}

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

相关主题
设计card deck问题,还有shuffle function,大家要搞清楚呀请问设计一个虚拟的动物园,用什么设计模式好啊?
求介绍设计parking lot的文档one C/C++ question
Amazon 两轮电话面经 及 design问题请教C++ Q47: protected constructor (C39)
进入JobHunting版参与讨论
w****x
发帖数: 2483
21

既然type是beverage的属性,为啥不直接合到beverage的object里呢

【在 d****o 的大作中提到】
: class vendingMachine{
: private:
: hash_map > m;
: public:
: bool getBeverage(int type){
: if(m.find(type)==m.end()){
: return false;
: }
: vector& cur = m[type];
: if(cur.size()==0){

d**e
发帖数: 6098
22
vending machine应该没有type吧。整个vending machine就像一个map List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
是选D1,D2,D3....如果这里面放的是coke.
Map> m_beverages;
boolean getBeverage(String bucket_id) {
if (!m_beverages.containKey(bucket_id)
|| m_beverages.get(bucket_id).isEmpty()) {
return false;
}
process(m_beverages.get(bucket_id).remove(0));
return true;
}

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

w****x
发帖数: 2483
23

对啊,太穷了,Vending Machine只见过,从来没用过, 无语了....

【在 d**e 的大作中提到】
: vending machine应该没有type吧。整个vending machine就像一个map: List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
: 是选D1,D2,D3....如果这里面放的是coke.
: Map> m_beverages;
: boolean getBeverage(String bucket_id) {
: if (!m_beverages.containKey(bucket_id)
: || m_beverages.get(bucket_id).isEmpty()) {
: return false;
: }
: process(m_beverages.get(bucket_id).remove(0));

d**e
发帖数: 6098
24
如果signature一定要是bool/boolean getBeverage(int),那就将int转化一下,或者c
++ map的key可以是int吧。

【在 d**e 的大作中提到】
: vending machine应该没有type吧。整个vending machine就像一个map: List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
: 是选D1,D2,D3....如果这里面放的是coke.
: Map> m_beverages;
: boolean getBeverage(String bucket_id) {
: if (!m_beverages.containKey(bucket_id)
: || m_beverages.get(bucket_id).isEmpty()) {
: return false;
: }
: process(m_beverages.get(bucket_id).remove(0));

w****x
发帖数: 2483
25

者c
signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
button, 就想到是type, 其实区分的还是slot

【在 d**e 的大作中提到】
: 如果signature一定要是bool/boolean getBeverage(int),那就将int转化一下,或者c
: ++ map的key可以是int吧。

d**e
发帖数: 6098
26
说起来,好像的确有这一种的vending machine。但我觉得还是要看哪些slot对应是哪
些type,选某种type,就到预先设定的slot找,返回第一支饮料。

【在 w****x 的大作中提到】
:
: 者c
: signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
: button, 就想到是type, 其实区分的还是slot

s***0
发帖数: 117
27
Interviewer: Implement the bool getItem (ItemType) function of a Vending
Machine.
So you should know that this is a trick question: It's too easy.
What he really says is: design a set of classes so you can easily implement
the above function.
He'll throw stuff in later like: what if the items in a vending machine are
not fixed? (e.g. vending machine held sodas, now it holds chips).
What if instead of bool getItem(), I want to return the price of the item?
Can your class handle of that easily with no major changes to the design of
the Vending machine class. (You can see my earlier code for an example of
such a class)
The question he asked here is very open ended. He's trying to get an
impression of how you would approach the problem and your view of software
engineering.
A great answer is to see the big picture and come up with a flexible design
right away.
A decent answer is to see some of the challenges but not come up with the
right classes (can turn into a great answer depending on follow ups)
A bad answer is to just implement the function (turns into decent answer if
you get the follow up questions).
A horrible answer is if you can't even come up with the function.
e****e
发帖数: 418
28
Show Ugly
class VendingMachine {

public boolean getItem(ItemType it) {
return Arrays.asList( ItemType.values() ).contains( it );
}

public float getPrice(ItemType it) {
return it.getPrice();
}
}
enum ItemType {
Coke (1.75f),
Water (1.5f),
Chip (1.2f);

private float price;

ItemType (float price) {
this.price = price;
}

public float getPrice() {
return this.price;
}
}

implement
are
of

【在 s***0 的大作中提到】
: Interviewer: Implement the bool getItem (ItemType) function of a Vending
: Machine.
: So you should know that this is a trick question: It's too easy.
: What he really says is: design a set of classes so you can easily implement
: the above function.
: He'll throw stuff in later like: what if the items in a vending machine are
: not fixed? (e.g. vending machine held sodas, now it holds chips).
: What if instead of bool getItem(), I want to return the price of the item?
: Can your class handle of that easily with no major changes to the design of
: the Vending machine class. (You can see my earlier code for an example of

g*****e
发帖数: 282
29
各种饮料是class,有个property是price,通过public method可以修改price。通过
factory来得到某个drink class的instance。
switch case是应该避免的。实际工作中还要把那个object/class map到 database和
web service interface/restful上去

【在 w****x 的大作中提到】
:
: 者c
: signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
: button, 就想到是type, 其实区分的还是slot

w****x
发帖数: 2483
30
design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理?
相关主题
C++ Q52: (C6)Amamon onsite 面经
C++ Q54: enum (C12)有人来讨论下OOD的题目通常怎么答吗?
uber 的一个面试题几个OOP的面试题
进入JobHunting版参与讨论
g**e
发帖数: 6127
31
能把m_beverages改成Map吗

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

w****x
发帖数: 2483
32

就是你们家的题啊。 有的时候感觉就是要知道子类类型,就是一个trade off.
当然可以改成map啊

【在 g**e 的大作中提到】
: 能把m_beverages改成Map吗
n******n
发帖数: 567
33
factory??
g**e
发帖数: 6127
34
Map> m_beverage;
boolean getBeverage(Type ntype) {
if (m_beverage.contains(ntype)
&& m_beverage.get(ntype).size()>0) {
m_beverage.get(ntype).remove(0);
return true;
} else {
return false;
}
}
public enum Type {
COKE, PEPSI, ...
}
为什么不能知道子类型?买东西的时候总要选一个,把类型enum pass进来就行了

【在 w****x 的大作中提到】
:
: 就是你们家的题啊。 有的时候感觉就是要知道子类类型,就是一个trade off.
: 当然可以改成map啊

w****x
发帖数: 2483
35

你这和我的做法没太大区别啊, 同样是定义了类型.
根据下面的A家面试指南是一个大大的red flag.
https://sites.google.com/site/steveyegge2/five-essential-phone-screen-
questions
"They may produce an elaborate class tree for Animals, and then declare an
enum ("Lion = 1, Bear = 2", etc.) to represent the type of each animal."

【在 g**e 的大作中提到】
: Map> m_beverage;
: boolean getBeverage(Type ntype) {
: if (m_beverage.contains(ntype)
: && m_beverage.get(ntype).size()>0) {
: m_beverage.get(ntype).remove(0);
: return true;
: } else {
: return false;
: }
: }

g**e
发帖数: 6127
36
我以为你说的enum是给定的? 另外我觉得这题不需要sub class。实际上我的code也没
用subclass啊
用一堆subclass加enum是不好,跟这哥们说的一样

【在 w****x 的大作中提到】
:
: 你这和我的做法没太大区别啊, 同样是定义了类型.
: 根据下面的A家面试指南是一个大大的red flag.
: https://sites.google.com/site/steveyegge2/five-essential-phone-screen-
: questions
: "They may produce an elaborate class tree for Animals, and then declare an
: enum ("Lion = 1, Bear = 2", etc.) to represent the type of each animal."

w****x
发帖数: 2483
37

这题本来就是考OOD的,参考一下career cup 150上OOD的章节的答案都是过度设计,
本来出的面试题基本上都是简单的场景, 其实所有这种面试题类型都一个class搞定,
但是这么做的话又怕面试官说没有OO的思维, 分寸真难把握。

【在 g**e 的大作中提到】
: 我以为你说的enum是给定的? 另外我觉得这题不需要sub class。实际上我的code也没
: 用subclass啊
: 用一堆subclass加enum是不好,跟这哥们说的一样

t******k
发帖数: 64
38
Why they have this enum in the first place? They already made a design
decision for you. It is therefore a reasonable question to ask why they did
so. Likely they will say, this is in response to, for example, a customer
press a button. Then, in that case, let each subclass of Beverage respond to
a representation of that button.

【在 w****x 的大作中提到】
:
: 这题本来就是考OOD的,参考一下career cup 150上OOD的章节的答案都是过度设计,
: 本来出的面试题基本上都是简单的场景, 其实所有这种面试题类型都一个class搞定,
: 但是这么做的话又怕面试官说没有OO的思维, 分寸真难把握。

i***e
发帖数: 452
39
这种题真不知道如果设计可以过关了。。 A家贼喜欢问这类问题, 每次设计完了都感
觉不得要领。。 按说类型应该算是这个class的一个property才对, 那样的话就不应
subclass吧。
S******1
发帖数: 269
40
当初挂在这道题目上了。。。
相关主题
几个OOP的面试题帮忙看个南加州小公司会计offer
Amazon InterviewMicrosoft Beijing Recruiting (转载)
OOD问题总结大G面试 on-site报销饭钱这么抠门?
进入JobHunting版参与讨论
s***0
发帖数: 117
41
class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};

const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
// The Number of slots on a vending machine
// And the path to some xml file
class VendingRule
{
public:
// constructor from xml
VendingRule(boost::filesystem::path pathToXml, unsigned int slots);
// return the item goes in what slot given slot number.
// Instantiation of the item goes here.
VendingItem::Ptr getItem(unsigned int slot);
}
class VendingMachine
{
private:
// A vending machine can hold 10s of thousands of different items
// but the number of slot is fixed.
const unsigned int mSlots;

// how many items each slot holds
const unsigned int mSlotHold;
// A Vending machine holds the rule
mutable VendingRule mVendingRule;

// And here's a map of slot, item.

typedef map ItemMap;
ItemMap mItemMap;

public:
// Constructor, set config for this machine
VendingMachine(unsigned int slots,
unsigned int slotHold,
VendingRule vendingRule)
:mSlots(slots), mSlotHold(slotHold), mVendingRule(vendingRule)
{
fillItemMap();
}
void fillItemMap()
{
for (unsigned int i = 0; i < mSlots ++i)
{
for (unsigned int j = 0; j {
VendingItem::Ptr vendingItem;
// get the item from the rule
mItemMap[Point(i,j)] = new(mVendingRule.getItem(i));
}
}
}

unsigned int getItem(unsigned int slot)
{
// You can use a variety of logic here.
// to do the counting.
return priceInCents;
// return the price, throw exception if empty.
// have try-catch on the other side ready to handle.
}
// vending machine should be able to have new rules.
void changeRule(VendingRule vendingRule);
// you can do other stuff too
// e.g. if it's an online vending macnine
// it should be multi-threaded.

// it should talk to a bank to deposit it's money
// it should be able to request a refill of low items
// it may even have machine learning to request most
// popular items in most popular slots.
};
s*******n
发帖数: 344
42
典型的over design

★ 发自iPhone App: ChineseWeb 7.5

【在 s***0 的大作中提到】
: class VendingItem
: {
: public:
: typedef boost::shared_ptr Ptr;
: enum ItemName
: {
: CocaCola;
: Chips;
: Cookies;
: Condoms;

w****x
发帖数: 2483
43

要这么做的话获取price的时候就是要用switch case这样的,这样的情况肯定是典型的
polymorphism, 设计的少了怕体现不出OO思维(毕竟是考OOD的,就丢一个class感觉有
点冒险), 设计多了怕over design, 觉得没有完美纯粹的OOD。 感觉的确不好把握。

【在 i***e 的大作中提到】
: 这种题真不知道如果设计可以过关了。。 A家贼喜欢问这类问题, 每次设计完了都感
: 觉不得要领。。 按说类型应该算是这个class的一个property才对, 那样的话就不应
: subclass吧。

s***0
发帖数: 117
44
elaborate?

【在 s*******n 的大作中提到】
: 典型的over design
:
: ★ 发自iPhone App: ChineseWeb 7.5

g**e
发帖数: 6127
45
所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
invent and simplify, vocally self critical,bias of action, have backbone;
disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
但是你的设计要能体现出你具备上述几点素质

都感
不应

【在 w****x 的大作中提到】
:
: 要这么做的话获取price的时候就是要用switch case这样的,这样的情况肯定是典型的
: polymorphism, 设计的少了怕体现不出OO思维(毕竟是考OOD的,就丢一个class感觉有
: 点冒险), 设计多了怕over design, 觉得没有完美纯粹的OOD。 感觉的确不好把握。

i***e
发帖数: 452
46
也是, 感觉这方面的能力需要一定的经验才能收放自如了。。不是光靠做几道题能解
决的

;

【在 g**e 的大作中提到】
: 所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
: ,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
: invent and simplify, vocally self critical,bias of action, have backbone;
: disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
: 但是你的设计要能体现出你具备上述几点素质
:
: 都感
: 不应

h*******e
发帖数: 1377
47
up 一下。。正在做这类的题。。感觉几个经典的一个是parking lot再一个 vending
machine在一个就是 elevator了。。应该要不断和 考官沟通吧。
w****x
发帖数: 2483
48

;
恩, 学习了, 早知道就好了

【在 g**e 的大作中提到】
: 所以一定要不停的交流,不停的问对方的想法。给出各种option的优缺点,更重要的是
: ,给出你为什么选择这种方法的理由。请参考amzn leadership principle里的几条,
: invent and simplify, vocally self critical,bias of action, have backbone;
: disagree and commit. 设计问题的时候这几条都是考察重点。设计题没有标准答案,
: 但是你的设计要能体现出你具备上述几点素质
:
: 都感
: 不应

d****o
发帖数: 1055
49
class vendingMachine{
private:
hash_map > m;
public:
bool getBeverage(int type){
if(m.find(type)==m.end()){
return false;
}
vector& cur = m[type];
if(cur.size()==0){
return false;
}
cur.pop_back();
return true;
}
}

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

w****x
发帖数: 2483
50

既然type是beverage的属性,为啥不直接合到beverage的object里呢

【在 d****o 的大作中提到】
: class vendingMachine{
: private:
: hash_map > m;
: public:
: bool getBeverage(int type){
: if(m.find(type)==m.end()){
: return false;
: }
: vector& cur = m[type];
: if(cur.size()==0){

相关主题
南湾区San Jose公司需要招聘一名办公室文员求介绍设计parking lot的文档
南湾区San Jose公司需要招聘一名办公室文员Amazon 两轮电话面经 及 design问题请教
设计card deck问题,还有shuffle function,大家要搞清楚呀请问设计一个虚拟的动物园,用什么设计模式好啊?
进入JobHunting版参与讨论
d**e
发帖数: 6098
51
vending machine应该没有type吧。整个vending machine就像一个map List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
是选D1,D2,D3....如果这里面放的是coke.
Map> m_beverages;
boolean getBeverage(String bucket_id) {
if (!m_beverages.containKey(bucket_id)
|| m_beverages.get(bucket_id).isEmpty()) {
return false;
}
process(m_beverages.get(bucket_id).remove(0));
return true;
}

【在 w****x 的大作中提到】
: design一个vending machine.
: class VendingMachine
: {
: public:
: ....
: bool getBeverage(int nType);
: private:
: vector m_beverages;
: };
: class Beverage;

w****x
发帖数: 2483
52

对啊,太穷了,Vending Machine只见过,从来没用过, 无语了....

【在 d**e 的大作中提到】
: vending machine应该没有type吧。整个vending machine就像一个map: List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
: 是选D1,D2,D3....如果这里面放的是coke.
: Map> m_beverages;
: boolean getBeverage(String bucket_id) {
: if (!m_beverages.containKey(bucket_id)
: || m_beverages.get(bucket_id).isEmpty()) {
: return false;
: }
: process(m_beverages.get(bucket_id).remove(0));

d**e
发帖数: 6098
53
如果signature一定要是bool/boolean getBeverage(int),那就将int转化一下,或者c
++ map的key可以是int吧。

【在 d**e 的大作中提到】
: vending machine应该没有type吧。整个vending machine就像一个map: List>,一般的vending machine没有给你选coke,反正我是没见过了。一般就
: 是选D1,D2,D3....如果这里面放的是coke.
: Map> m_beverages;
: boolean getBeverage(String bucket_id) {
: if (!m_beverages.containKey(bucket_id)
: || m_beverages.get(bucket_id).isEmpty()) {
: return false;
: }
: process(m_beverages.get(bucket_id).remove(0));

w****x
发帖数: 2483
54

者c
signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
button, 就想到是type, 其实区分的还是slot

【在 d**e 的大作中提到】
: 如果signature一定要是bool/boolean getBeverage(int),那就将int转化一下,或者c
: ++ map的key可以是int吧。

d**e
发帖数: 6098
55
说起来,好像的确有这一种的vending machine。但我觉得还是要看哪些slot对应是哪
些type,选某种type,就到预先设定的slot找,返回第一支饮料。

【在 w****x 的大作中提到】
:
: 者c
: signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
: button, 就想到是type, 其实区分的还是slot

s***0
发帖数: 117
56
Interviewer: Implement the bool getItem (ItemType) function of a Vending
Machine.
So you should know that this is a trick question: It's too easy.
What he really says is: design a set of classes so you can easily implement
the above function.
He'll throw stuff in later like: what if the items in a vending machine are
not fixed? (e.g. vending machine held sodas, now it holds chips).
What if instead of bool getItem(), I want to return the price of the item?
Can your class handle of that easily with no major changes to the design of
the Vending machine class. (You can see my earlier code for an example of
such a class)
The question he asked here is very open ended. He's trying to get an
impression of how you would approach the problem and your view of software
engineering.
A great answer is to see the big picture and come up with a flexible design
right away.
A decent answer is to see some of the challenges but not come up with the
right classes (can turn into a great answer depending on follow ups)
A bad answer is to just implement the function (turns into decent answer if
you get the follow up questions).
A horrible answer is if you can't even come up with the function.
e****e
发帖数: 418
57
Show Ugly
class VendingMachine {

public boolean getItem(ItemType it) {
return Arrays.asList( ItemType.values() ).contains( it );
}

public float getPrice(ItemType it) {
return it.getPrice();
}
}
enum ItemType {
Coke (1.75f),
Water (1.5f),
Chip (1.2f);

private float price;

ItemType (float price) {
this.price = price;
}

public float getPrice() {
return this.price;
}
}

implement
are
of

【在 s***0 的大作中提到】
: Interviewer: Implement the bool getItem (ItemType) function of a Vending
: Machine.
: So you should know that this is a trick question: It's too easy.
: What he really says is: design a set of classes so you can easily implement
: the above function.
: He'll throw stuff in later like: what if the items in a vending machine are
: not fixed? (e.g. vending machine held sodas, now it holds chips).
: What if instead of bool getItem(), I want to return the price of the item?
: Can your class handle of that easily with no major changes to the design of
: the Vending machine class. (You can see my earlier code for an example of

g*****e
发帖数: 282
58
各种饮料是class,有个property是price,通过public method可以修改price。通过
factory来得到某个drink class的instance。
switch case是应该避免的。实际工作中还要把那个object/class map到 database和
web service interface/restful上去

【在 w****x 的大作中提到】
:
: 者c
: signature没限制, 随便写的, 当时想到饮料机都是可乐一个button, 雪碧一个
: button, 就想到是type, 其实区分的还是slot

l**b
发帖数: 457
59
Mark
1 (共1页)
进入JobHunting版参与讨论
相关主题
one C/C++ questionAmazon Interview
C++ Q47: protected constructor (C39)OOD问题总结
C++ Q52: (C6)帮忙看个南加州小公司会计offer
C++ Q54: enum (C12)Microsoft Beijing Recruiting (转载)
uber 的一个面试题大G面试 on-site报销饭钱这么抠门?
Amamon onsite 面经南湾区San Jose公司需要招聘一名办公室文员
有人来讨论下OOD的题目通常怎么答吗?南湾区San Jose公司需要招聘一名办公室文员
几个OOP的面试题设计card deck问题,还有shuffle function,大家要搞清楚呀
相关话题的讨论汇总
话题: beverage话题: vending话题: int话题: unsigned话题: class