Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
User Registration and Management:
System must allow users to register and manage their profiles.
Users can update their contact information, such as email address, phone number, etc.
Notification Preferences:
Users should be able to set and update their notification preferences (e.g., preferred notification channels and frequency).
System should allow opting in and out of certain types of notifications.
Notification Channels:
Support multiple notification channels like email, SMS, push notifications, etc.
Ability to configure multiple endpoints for different channels.
Notification Scheduling:
Schedule notifications for future delivery at a specific date and time.
Manage recurring notifications with defined intervals.
Based on the requirements and use cases, identify the main objects of the system...
Entities User,Notification whatsapp, email,sms, Scheduling
Determine how these objects will interact with each other to fulfill the use cases...
Each User has Channel using which he can send different types of notification like sms, whatsapp, email and each notification class has a refernece to Schedule class that will store the schedule details like the montly, weekly and type of schedule
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
This design has used only strategy pattern for scheduleing different types of messages.
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
import java.time.*;
import java.util.*;
enum ChannelType { EMAIL, SMS, WHATSAPP }
class User {
private final UUID id;
private String firstName, lastName, email, phoneNumber;
public User(UUID id, String firstName, String lastName, String email, String phoneNumber) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
}
public String getFullName() { return firstName + " " + lastName; }
public String getEmail() { return email; }
public String getPhoneNumber(){ return phoneNumber; }
public void updateEmail(String email) { this.email = email; }
public void updatePhoneNumber(String phone) { this.phoneNumber = phone; }
}
class NotificationPreference {
private final User user;
private final ChannelType channelType;
private final boolean enabled;
private final Schedule schedule;
public NotificationPreference(User user,
ChannelType channelType,
boolean enabled,
Schedule schedule) {
this.user = user;
this.channelType = channelType;
this.enabled = enabled;
this.schedule = schedule;
}
public User getUser() { return user; }
public ChannelType getChannelType() { return channelType; }
public boolean isEnabled() { return enabled; }
public Schedule getSchedule() { return schedule; }
}
class Notification {
private final User user;
private final String content;
private final Channel channel;
public Notification(User user, String content, Channel channel) {
this.user = user;
this.content = content;
this.channel = channel;
}
public void dispatch() {
channel.send(user, content);
}
}
interface Channel {
void send(User user, String content);
}
class EmailChannel implements Channel {
@Override
public void send(User user, String content) {
System.out.printf("[EMAIL] to %s <%s>: %s%n",
user.getFullName(), user.getEmail(), content);
}
}
class SmsChannel implements Channel {
@Override
public void send(User user, String content) {
System.out.printf("[SMS] to %s (%s): %s%n",
user.getFullName(), user.getPhoneNumber(), content);
}
}
class WhatsappChannel implements Channel {
@Override
public void send(User user, String content) {
System.out.printf("[WHATSAPP]to %s (%s): %s%n",
user.getFullName(), user.getPhoneNumber(), content);
}
}
class ChannelFactory {
public static Channel getChannel(ChannelType type) {
switch(type) {
case EMAIL: return new EmailChannel();
case SMS: return new SmsChannel();
case WHATSAPP: return new WhatsappChannel();
default:
throw new IllegalArgumentException("Unknown channel: " + type);
}
}
}
interface Schedule {
boolean isTimeToSend(LocalDateTime now);
}
class OneTimeSchedule implements Schedule {
private final LocalDateTime sendAt;
public OneTimeSchedule(LocalDateTime sendAt) {
this.sendAt = sendAt;
}
@Override
public boolean isTimeToSend(LocalDateTime now) {
return !now.isBefore(sendAt);
}
}
class DailySchedule implements Schedule {
private final LocalTime timeOfDay;
public DailySchedule(LocalTime timeOfDay) {
this.timeOfDay = timeOfDay;
}
@Override
public boolean isTimeToSend(LocalDateTime now) {
LocalTime t = now.toLocalTime();
return t.getHour() == timeOfDay.getHour()
&& t.getMinute() == timeOfDay.getMinute();
}
}
class WeeklySchedule implements Schedule {
private final DayOfWeek dayOfWeek;
private final LocalTime timeOfDay;
public WeeklySchedule(DayOfWeek dayOfWeek, LocalTime timeOfDay) {
this.dayOfWeek = dayOfWeek;
this.timeOfDay = timeOfDay;
}
@Override
public boolean isTimeToSend(LocalDateTime now) {
return now.getDayOfWeek() == dayOfWeek
&& now.toLocalTime().getHour() == timeOfDay.getHour()
&& now.toLocalTime().getMinute() == timeOfDay.getMinute();
}
}
class NotificationService {
private final List<NotificationPreference> preferences;
public NotificationService(List<NotificationPreference> prefs) {
this.preferences = prefs;
}
public void dispatchNotifications(String content) {
LocalDateTime now = LocalDateTime.now();
for (NotificationPreference pref : preferences) {
if (!pref.isEnabled()) continue;
if (pref.getSchedule().isTimeToSend(now)) {
Channel channel = ChannelFactory.getChannel(pref.getChannelType());
Notification note = new Notification(pref.getUser(), content, channel);
note.dispatch();
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
User alice = new User(
UUID.randomUUID(),
"Alice", "Smith",
"[email protected]",
"+1-800-555-0100"
);
NotificationPreference oneTimeEmail = new NotificationPreference(
alice,
ChannelType.EMAIL,
true,
new OneTimeSchedule(LocalDateTime.now().plusSeconds(1))
);
LocalDateTime now = LocalDateTime.now();
LocalTime nextMinute = now.plusMinutes(1).withSecond(0).withNano(0);
NotificationPreference dailySms = new NotificationPreference(
alice,
ChannelType.SMS,
true,
new DailySchedule(nextMinute.toLocalTime())
);
List<NotificationPreference> prefs = Arrays.asList(oneTimeEmail, dailySms);
NotificationService service = new NotificationService(prefs);
System.out.println("Starting dispatch loop (runs every 15s)...");
for (int i = 0; i < 8; i++) {
service.dispatchNotifications("👋 Hello, this is your notification!");
Thread.sleep(15_000);
}
System.out.println("Done.");
}
}
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Single Responsibilty Principle The above design adheras to single responsibitly principle as each class has only one responsibitly
Open can closed principle - > The above design allows class to be extended to add extra functionality instead of adding it in the same class
Liskov Substitution Priciple- > All child classes can be substituted in place of their parent class
Interface Segeration principle -> Interface implement only the required functionality
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
The above design follows solid principles and design pattern so it is higly scalable
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...