找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

12
返回列表 发新帖
楼主: Powerful
收起左侧

[Bloomberg] Bloomberg面经Senior职位

[复制链接]

1154

主题

153

精华

3407

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3407
发表于 2-5-2017 02:45 PM | 显示全部楼层

I think they wanted to see how you would design this with your own classes. Using your own classes also means you can easily keep track of the highest value / lowest value in a listing as you consume the stream, getRatio() can then be O(1).

e.g.

public class StockCompany {; B. |' [: s$ ?9 {  Y/ b

/ J, Y) |- F, a6 M' H: ~! l. e    private final String name;) x1 k: s9 B3 R9 _6 `

, m+ M$ H' ?2 z; d; K4 F    public StockCompany(String name) {- `6 z, w. p2 d8 I
        this.name = name;& @5 Q6 k2 j; n# F5 R0 T
    }$ g# z7 h1 L- w5 k3 |/ j  g
* i- t+ a( J+ D1 G; {# f7 H/ f
    public String getName() {
( H' [0 x( v* ~2 S1 [4 H0 d        return name;/ z$ }" _; Q+ O9 o, j* v$ n2 {
    }! B2 \+ S7 S0 r. g0 ~
}

public class StockListing {+ _" D6 K% t+ S. p
; P. W/ U4 C& B  L
    private final StockCompany company;
# H' c: U! d5 K' P* V" Y    private final List<StockValue> values;8 U( W* {7 M7 H
9 v7 X; k5 B: A! C
    private StockValue highestStockValue;% o, v+ N8 u$ n+ {
    private StockValue lowestStockValue;/ P- d9 C" l  r! s2 C
0 W, l: w/ V. c. V  Q
    public StockListing(StockCompany company, StockValue initialStockValue) {
) E8 ^# a3 @/ U2 e1 h3 s$ H        this.company = company;! R1 V0 q' b& k4 f7 _8 L
        this.values = new ArrayList();
# I' [3 |5 W9 B; q: y0 f+ L8 u        if(initialStockValue == null) {# e5 L+ A6 t# D) B/ b
            throw new StockListingInitialValueNullException();$ W) h# F' x& F/ x. B
        }
3 t0 s- P- z6 i& r1 [0 ~6 b: C        this.values.add(initialStockValue);
& y! p' ~* j& F2 E$ [. f        highestStockValue = initialStockValue;
6 J0 Y3 h. }2 l        lowestStockValue = initialStockValue;
% A- I" y, i' Y, ^& ?    }. @7 L0 U4 q6 U* T0 Y

0 v% Q6 J) d  h  P' a# [    public StockCompany getCompany() {
/ U9 {) d( z& M        return company;) P! [* q$ [( I6 l
    }4 I8 o1 C$ |" L* {# L
' w- c; K+ t" @3 |9 W7 D3 `
    public List<StockValue> getValues() {
4 f* W+ `+ R( u$ f        return Collections.unmodifiableList(values);
# q  l: n% F) U! b& X" X9 l    }/ \) g7 a1 n- w6 v) K) }/ a
0 U9 ~) o$ J6 C! [; m) t
    public void addValue(StockValue stockValue) {1 S$ M5 U; Q) N' E* H; t- B
        this.values.add(stockValue);* a: R) ]( G0 ]. \/ v
        if(stockValue.getValue() > highestStockValue.getValue()) {& ]1 h. A* L- |
            highestStockValue = stockValue;
7 L7 m& T9 j& `. |' M+ t3 b1 I# @        } else if(stockValue.getValue() < lowestStockValue.getValue()) {
7 \, n; s8 m: A7 J. ]- P            lowestStockValue = stockValue;6 h- o9 }  c0 W# x
        }' i( P* R/ n. y
    }7 y* _# R' L/ p9 a

2 z; Q. X- y  j. W- ]    public float getLowestToHighestRatio() {/ @6 _( I/ |9 P1 Y3 R( I. N
        return lowestStockValue.getValue() / highestStockValue.getValue();! e5 s4 q7 m6 y; y; k0 c
    }
) J  e; h4 O# i+ ]}

public class StockValue {
* o1 ~0 V$ n' r& K7 V4 \" `" m; g, C' T. ^
    private final float value;
2 S7 d5 u4 A( e& |- K6 Z' b) {! _' K) m/ c6 J5 K
    public StockValue(float value) {7 O' ^% K; C2 L
        this.value = value;
, S7 m8 ]. P3 ^* ]    }
' v4 A& _& L! h2 _3 q; Y7 [& y$ T# |( f: r4 Y7 {$ p- L
    public float getValue() {) y7 B' K+ L7 s" F6 q5 D9 U7 B: q
        return value;) t$ @4 j/ s# X- m% C$ U/ C3 b
    }
; C8 p) P! ]5 f9 ~8 k( W  F; `}

public class StockListingInitialValueNullException extends RuntimeException {
" N1 r' c# s* s7 A& i. V1 y$ ]
2 B$ |: @/ ~$ {" ~/ z4 s" F2 }5 C    public StockListingInitialValueNullException() {
/ ?* \% r+ `9 M0 l2 {        super("Stock listing can not have a null initial stock value");; R0 Q7 c- ^% W" y2 a& `
    }
- M6 w6 O, ^6 h8 J- `8 j8 S) Z
}

0

主题

0

精华

3

积分

新米人

Rank: 1

积分
3
发表于 2-6-2017 10:12 PM | 显示全部楼层
感谢Powerful发帖~~~

1152

主题

175

精华

3553

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3553
发表于 2-6-2017 10:16 PM | 显示全部楼层
好帖~~~学习了~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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