Design a notification factory for email, SMS and push messages
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design and implement a `NotificationFactory` that uses the Factory design pattern. The factory creates different types of notification objects (Email, SMS or Push) from a string type identifier.
Each notification has a recipient, a message, and a unique id assigned by the factory. A notification can be sent, which produces a formatted string describing the delivery. The factory also tracks how many notifications of each type have been created, keeps a creation history, and reports the status of any notification.
Implement the following methods:
- `createNotification(ntype: str, recipient: str, message: str) -> int` is the factory method. It creates a notification of the given type (`'email'`, `'sms'` or `'push'`) with the given recipient and message and returns its id, or `-1` if the type is invalid. Type matching is case-insensitive.
- `send(nid: int) -> str` sends the notification with the given id and returns a string of the form `'<Channel> sent to <recipient>: <message>'`, where the channel is `Email`, `SMS` or `Push`. It returns `'Invalid'` if the id does not exist, or `'Already Sent'` if the notification was already sent.
- `getCount(ntype: str) -> int` returns the total number of notifications created of the given type. Type matching is case-insensitive.
- `getHistory() -> List[int]` returns all notification ids in the order they were created.
- `getStatus(nid: int) -> str` returns `'Sent'` if the notification was already sent, `'Pending'` if it was created but not sent, or `'Not Found'` if the id does not exist.
Example:
```text
createNotification("Email", "alice@example.com", "Welcome") -> 0
createNotification("SMS", "555-0100", "Your code is 4821") -> 1
createNotification("fax", "bob", "Hello") -> -1
createNotification("push", "device-7", "New reply") -> 2
getCount("EMAIL") -> 1
getCount("fax") -> 0
send(0) -> "Email sent to alice@example.com: Welcome"
send(0) -> "Already Sent"
send(5) -> "Invalid"
getStatus(0) -> "Sent"
getStatus(1) -> "Pending"
getStatus(9) -> "Not Found"
getHistory() -> [0, 1, 2]
```
`"fax"` is not a known type, so that call returns `-1` and the next notification still gets id 2. `getCount("EMAIL")` counts the notification created as `"Email"` because type matching ignores case. The second `send(0)` finds the notification already sent, and `send(5)` and `getStatus(9)` refer to ids that were never created.
```hint One place for the type
Aim for a single spot in the code that knows which type string maps to which kind of notification. Then check whether `send` still needs to look at the type at all.
```
### Constraints & Assumptions
- `ntype` is one of `'email'`, `'sms'`, `'push'` (in any letter case) for valid notifications.
- `1 <= len(recipient) <= 100`
- `1 <= len(message) <= 500`
- At most `1000` total method calls.
- Ids start at 0 and go up by one with each successful creation. A call with an invalid type does not use up an id.
### Clarifying Questions to Ask
- Should the factory itself own the counts, history and sent status, or is it fine to split creation from tracking?
- Should the type string be trimmed of surrounding whitespace, or does only letter case get ignored?
- Can a delivery fail, and if so, is there a status beyond `Pending` and `Sent`?
### What a Strong Answer Covers
- How the concrete notification types share one interface, and where the type string is mapped to a concrete class.
- Case-insensitive type handling in both `createNotification` and `getCount`, and the result for an unknown type.
- How ids, per-type counts, creation history and sent status are stored, and the time cost of each method.
- The `Invalid`, `Already Sent` and `Not Found` paths.
- What code changes when a new channel is added.
### Follow-up Questions
- How would you add a Slack channel, and which existing code would have to change?
- The factory also stores, counts and sends notifications. Would you split those responsibilities, and how?
- How would you add retries and a `Failed` status when the provider call behind `send` can fail?
- How would you keep id assignment and counting correct if several threads create notifications at the same time?
Overview: Design a notification factory that creates email, SMS and push notifications from a type string, assigns ids, and reports per-type counts, creation history and sent status. It tests the Factory pattern, case-insensitive input handling, and keeping type-specific behavior out of the bookkeeping code.