AlgoMaster Logo
AlgoMasterDesign Stock Price Alertseasy

Design Stock Price Alerts

easy

A stock ticker pushes every new price for one symbol to the observers registered with it. A display shows the latest price, and an alert records every price that reaches its limit. The ticker knows only the shared PriceObserver contract.

Implement these parts:

  • PriceObserver is the observer contract. It exposes onPrice(symbol, price).
  • PriceDisplay and PriceAlert implement that contract. PriceAlert is constructed with its limit.
  • StockTicker is the subject, and its skeleton is in the starter. addObserver(PriceObserver observer) registers an object once, removeObserver(PriceObserver observer) removes that same object, and both return whether they changed the registration. setPrice(price) calls onPrice on every observer in registration order and returns how many were notified. The ticker must not construct observers, look them up by name, compare prices against limits, or keep a price history.
  • The provided StockTickerFacade is the driver called by the tests. It translates the string commands below into observer construction, registration, removal, and reporting. Do not rewrite it; implement the types it delegates to.

The provided StockTickerFacade exposes this API:

  • StockTickerFacade(String symbol) creates a facade around a ticker for that symbol with no observers.
  • boolean addDisplay() registers the price display and returns true. A second call changes nothing and returns false.
  • boolean addAlert(String name, double limit) registers an alert under that name and returns true. A name already in use changes nothing and returns false.
  • boolean removeAlert(String name) removes that alert and returns true, or returns false when no alert has that name.
  • int setPrice(double price) sends the price to every registered observer in registration order and returns how many received it.
  • String display() returns "AAPL: 152.50" for the latest price the display received, or "NONE" when the display is not registered or has received no price.
  • String[] alertLines(String name) returns every line that alert has recorded, oldest first, or an empty array when no alert has that name.
  • int observerCount() returns how many observers are registered.

An alert records AAPL crossed 150.00 at 152.50 for every price at or above its limit, including a price exactly equal to the limit. Prices below the limit leave no trace.

Every number is formatted to two decimal places. An observer receives only the prices that arrive while it is registered, and removing an alert discards it, so adding the same name again starts with no lines.

StockTickerFacade is the only type the tests call. Keep its string-based bookkeeping outside StockTicker: the ticker should push prices through PriceObserver without knowing alert names, concrete classes, or limits.

Example 1:

Input:

Output:

Explanation: Both observers receive both prices. The display keeps the latest one, and the alert records a line only for the price at or above its limit.

Example 2:

Input:

Output:

Explanation: The display registers after the first price has gone out, so it reports NONE until the second price arrives. The alert saw both prices and fired on the first.

Constraints

  • 1 <= symbol.length <= 6, upper case letters only.
  • 1 <= name.length <= 20, lower case letters only.
  • 0.0 < price <= 100000.0 and 0.0 < limit <= 100000.0
  • At most 100 calls in total are made across all methods.

Starter Code

Implement the PriceObserver contract and its two concrete observers, then complete the StockTicker skeleton. StockTickerFacade is complete driver code and must not be modified.

How the design is graded

needs 7/10 to pass
  • Observer separation

    Full marks when `StockTicker` holds `PriceObserver` objects and pushes each price through that contract, while `StockTickerFacade` alone handles alert names and concrete construction. Lose points heavily when the ticker constructs an observer, looks one up by name, compares a price against a limit, or branches on an observer kind during notification.

  • Registration decides who receives what

    Full marks when an observer sees exactly the prices sent while it was registered, so a late display reports `NONE` until the next price and an alert removed and re-added starts with no lines. Lose points when an observer reads a price history kept by the ticker.

  • Structure and naming

    Full marks when notification follows registration order, a duplicate display or alert name is refused without changing anything, an alert fires for a price equal to its limit, and every number is formatted to two decimal places. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new StockTickerFacade("AAPL")null
addDisplay()true
addAlert("high", 150)true
setPrice(148)2
setPrice(152.5)2
display()"AAPL: 152.50"
alertLines("high")["AAPL crossed 150.00 at 152.50"]

Both observers receive both prices. The display keeps the latest one, and the alert records a line only for the price at or above its limit.

Run checks these cases. Submit also runs a larger hidden set.