找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2053|回复: 4
收起左侧

[Microsoft] Microsoft Interview for Intern

[复制链接]

1117

主题

164

精华

3414

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3414
发表于 6-7-2017 09:29 AM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

Design a stock market system

1161

主题

184

精华

3664

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3664
发表于 6-7-2017 09:29 AM | 显示全部楼层

Can make use of Observer Design Pattern. 5 a3 U. d4 t8 u% B7 @' p
Here is a simple implementation of the Publisher/Subscriber model for StockMarket. As and when interviewer pose with some more questions, we can add on to it./ C8 n' k2 |0 W( O3 D2 G
' F, P0 Q2 ~( |; h( W4 V: s
Publisher Interface:

public interface Publisher {4 e9 K$ |# a% V3 N
  public void setStockValue(int value);. _7 x3 Z2 E( Z2 V: v' h
  public void registerObserver(Observer observer);
( B2 {4 ?. }9 M% e% l) S1 r9 A  public void unRegisterObserver(Observer observer);$ I* k6 E0 Z0 }
  public void notifyAllObservers();) Y% ?# }/ U' q
}

Subscriber(Observer) Interface:

public interface Observer {6 ^0 ^9 ~8 P7 y* n3 v
   public void update(int newValue);
* T3 H/ R! G  k' j% K4 c   public void display();
9 t: K! |+ d# g7 Q8 ~   public void unSubscribe();* Y" O% z* Y* X; o5 r
}

StockMarket Class which implements the Publisher interface:

public class StockMarket : Publisher {5 p5 a4 s' _* b- q9 ]
  Set<Observer> observers;2 |/ L! h# j9 }# G+ Y
  int newValue;
: q9 Y! ?" C3 Y! D1 Q9 V
. P. A+ k6 f0 E, b' K1 V  public StockMarket() {# J3 v/ {! F2 }& C4 E
	observers = new HashSet<Observer>();
) {; ]! m5 R" V: V4 e	newValue = 0;
" }; L5 v: u* H# |  }  
% h# f, `7 @7 c2 q* t4 x/ d  8 K2 A5 A9 W) R4 J' K
  public void setStockValue(int value){
" _1 i- q0 `6 ?$ K    newValue = value;/ J* C9 f3 [5 I- _! D9 g6 x
  }( n5 r5 Q' r! h7 d
  
! Y9 d0 m& L, w0 w  public void registerObserver(Observer observer){
$ n% w9 i7 w- }7 g. K* P- W4 O    observers.add(observer);0 S3 N% v& S6 K7 W# ?5 M! Z8 _
  }& b$ a' G/ p# ?& G( @

; ~3 f, d. H; u$ z0 ^3 Q  public void unRegisterObserver(Observer observer){
2 U4 N( g+ k* x1 k0 @! L    if(observer != null && observer instanceof Observer){
0 n+ _, p! N* o& ~" X6 }	    observers.remove(observer);# F; x8 \8 [3 d7 g) C
	}0 U3 c; n7 d/ C1 Y5 s+ z- F, C
  }! Z% g+ d8 B  `' |% s
/ Y* W  v. r$ L. K9 s
  public void notifyAllObservers(){* u7 Q/ E- E- G
	Iterator<Observer> observerIterator = observers.iterator();
' x& ~  H, `! @, J+ C	while(observerIterator.hasNext()){% S& q' @, ^+ F2 a/ y! m
		Observer observer = observerIterator.next();
- `2 Y( U# G( M! u: Y		observer.update(this, null);+ v, n" V+ l% g2 x' u( j# I8 d  d
	}
4 {! x0 Q7 ^" P: ~. M% R( q( E  }
5 Z7 k( b! W, V4 m}

As a sample i will implement the PayPalStock which implements the Observer Interface. This inturn maintains a reference to the Publisher.

public class PayPalStock : Observer {" r% h8 A& ?, `7 E
   Publisher stockMarket;
& u( X# p1 Y$ o3 `) ?5 [   int latestValue;
, f' _, K( A: f' `9 C) Q5 Y7 w! p
! I6 E# z: H( D9 {0 S: Z" W   public PayPalStock (Publisher subject){- p' m1 E% x( Z
   	latestValue = 0;
* \- v- R4 c, m7 Y3 k/ F    stockMarket = subject;! V0 x" I' P4 j& O! h
    stockMarket.registerObserver(this); // Self-Registering to the Publisher6 O% g# y0 s' [$ G3 R
   }7 }" y# d1 g+ T0 M4 i
- s2 k3 ^0 @0 S* B6 L- i% A7 k/ K
   public void update(int newValue){% J0 p9 J# t% J# [% m
    latestValue = newValue;6 m1 R8 Y' O/ y5 h& _8 d* Z5 Q
    display();
& U- J8 E9 r( ?! D- J$ D   }
, ^; t1 L9 W5 r1 a& j4 @
6 `1 P/ r" e1 a4 F* Q! s$ S+ X- z2 N6 Z2 M   public void display(){9 y/ D/ b: A' V. {
     System.out.println("Latest Stock Price = " + latestValue);. y$ `1 U1 @- J9 x( K  I4 l7 J4 k9 ]
   }
# T5 L0 a/ ^6 _& l, \: g$ |1 B
' j! A3 y; j+ n% M/ p   public void unSubscribe(){. c+ M9 l; s5 B
     stockMarket.unRegisterObserver(this);" x* c1 i' q; x1 |% o" ?6 U9 z
   }
( ]) Y' F3 n4 \& K9 Q$ N8 E}

A more detailed design can be looked up at: http://read.pudn.com/downloads152/doc/663610/Software%20Design.pdf

1194

主题

195

精华

3819

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3819
发表于 6-7-2017 09:29 AM | 显示全部楼层

A market is a place where buy and sell happens real-time or in a batch. depending on the service... The code above won't cut....

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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