AlgoMaster Logo
AlgoMasterDesign Newsletter Publishereasy

Design Newsletter Publisher

easy

A newsletter publisher pushes every headline to the subscribers registered with it. Each subscriber turns the headline into a message for its own channel and keeps that message, and the publisher knows only the shared NewsSubscriber contract.

Implement these parts:

  • NewsSubscriber is the observer contract. It exposes receive(headline) and messages().
  • EmailSubscriber and SmsSubscriber implement that contract. Each is constructed with the subscriber's name.
  • NewsPublisher is the subject, and its skeleton is in the starter. subscribe(NewsSubscriber subscriber) registers an object once, unsubscribe(NewsSubscriber subscriber) removes that same object, and both return whether they changed the registration. publish(headline) calls receive on every subscriber in subscription order and returns how many were notified. The publisher must not construct subscribers, look them up by name, format messages, or keep headlines.
  • The provided NewsPublisherFacade is the driver called by the tests. It translates channel and name strings into subscriber construction, registration, removal, and inbox lookups. Do not rewrite it; implement the types it delegates to.

The provided NewsPublisherFacade exposes this API:

  • NewsPublisherFacade() creates a facade around an empty publisher.
  • boolean subscribe(String channel, String name) registers a subscriber on channel "email" or "sms" under name and returns true. An unknown channel, or a name that is already subscribed, changes nothing and returns false.
  • boolean unsubscribe(String name) removes that subscriber and returns true, or returns false when nobody is subscribed under that name.
  • int publish(String headline) sends the headline to every subscriber in subscription order and returns how many received it.
  • String[] inbox(String name) returns every message that subscriber has stored, oldest first, or an empty array when nobody is subscribed under that name.
  • int subscriberCount() returns how many subscribers are registered.

The two channels format differently:

  • email stores Email to alice: Rates cut
  • sms stores SMS to bob: Rates cut

A subscriber receives only the headlines published while it is subscribed, and unsubscribing discards it, so subscribing the same name again starts with an empty inbox.

NewsPublisherFacade is the only type the tests call. Keep its string-based bookkeeping outside NewsPublisher: the publisher should push headlines through NewsSubscriber without knowing channels, names, or message formats.

Example 1:

Input:

Output:

Explanation: Both subscribers are registered before the headline goes out, so both receive it. Each one formats the same headline for its own channel.

Example 2:

Input:

Output:

Explanation: Bob subscribes after the first headline has gone out, so it never reaches him. His inbox holds only the second headline while Alice has both.

Constraints

  • 1 <= headline.length <= 60
  • 1 <= name.length <= 20, lower case letters only.
  • channel is a lower case word of at most 10 characters.
  • At most 100 calls in total are made across all methods.

Starter Code

Implement the NewsSubscriber contract and its two concrete subscribers, then complete the NewsPublisher skeleton. NewsPublisherFacade is complete driver code and must not be modified.

How the design is graded

needs 7/10 to pass
  • Observer separation

    Full marks when `NewsPublisher` holds `NewsSubscriber` objects and pushes headlines through that contract, while `NewsPublisherFacade` alone handles channels, names and concrete construction. Lose points heavily when the publisher constructs a subscriber, looks one up by name, formats a message itself, or branches on the channel during publishing.

  • Subscription decides who receives what

    Full marks when a subscriber holds exactly the headlines published while it was subscribed, so a late subscription excludes everything earlier and a removal followed by a fresh subscription starts from nothing. Lose points when a subscriber reads a history kept by the publisher, or when subscribing hands it earlier headlines.

  • Structure and naming

    Full marks when notification follows subscription order, a duplicate name or unknown channel is refused without changing anything, `publish` returns the number of subscribers notified, and each channel formats its line exactly. 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 NewsPublisherFacade()null
subscribe("email", "alice")true
subscribe("sms", "bob")true
publish("Rates cut")2
inbox("alice")["Email to alice: Rates cut"]
inbox("bob")["SMS to bob: Rates cut"]

Both subscribers are registered before the headline goes out, so both receive it. Each one formats the same headline for its own channel.

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