Similar Problems

Similar Problems not available

Users That Actively Request Confirmation Messages - Leetcode Solution

Companies:

LeetCode:  Users That Actively Request Confirmation Messages Leetcode Solution

Difficulty: Easy

Topics: database  

Problem Statement:

The problem "Users That Actively Request Confirmation Messages" on LeetCode is given as follows:

Suppose you are implementing a user messaging system where users can send messages to other users. One feature you would like to implement is sending confirmation messages when a user sends a message to another user.

You need to implement the ConfirmChecker class:

  • ConfirmChecker(int) Initializes the object with the number of messages that can be sent per minute.
  • boolean sendMessage(int, int, String) Sends a message from the sender to the receiver with a message id and returns true if the message is allowed to be sent and false otherwise.
  • boolean receiveMessage(int, int, String) Receives a message from the sender with a message id and returns true if the message was sent to the receiver and false otherwise. The message is not guaranteed to be delivered in order. If a message cannot be delivered return false.

A sender can only send messages at most count per minute. Both the sender and receiver should have less than maxMessages messages in their inbox.

Implement the ConfirmChecker class:

Example:

Input: ["ConfirmChecker","sendMessage","receiveMessage","sendMessage","sendMessage","sendMessage","receiveMessage","sendMessage"] [[1],[0,1,"aaaa"],[1,0,"bbbb"],[1,2,"cccc"],[1,3,"dddd"],[0,2,"eeee"],[2,1,"ffff"],[2,3,"gggg"]]

Output: [null,true,true,true,true,false,false,true]

Explanation: ConfirmChecker confirmChecker = new ConfirmChecker(1); confirmChecker.sendMessage(0, 1, "aaaa"); // return true, User 0 sends a message "aaaa" to user 1 confirmChecker.receiveMessage(1, 0, "bbbb"); // return true, User 1 receives a message "bbbb" from user 0 confirmChecker.sendMessage(1, 2, "cccc"); // return true, User 1 sends a message "cccc" to user 2 confirmChecker.sendMessage(1, 3, "dddd"); // return true, User 1 sends a message "dddd" to user 3 confirmChecker.sendMessage(0, 2, "eeee"); // return false, User 0 tries to send a message "eeee" to user 2, but exceeds the number of messages per minute. confirmChecker.receiveMessage(2, 1, "ffff"); // return false, User 2 tries to receive a message "ffff" from user 1, but does not have any messages in their inbox. confirmChecker.sendMessage(2, 3, "gggg"); // return true, User 2 sends a message "gggg" to user 3

Solution:

The problem states that we are implementing a user messaging system where users can send messages to other users and we need to implement a ConfirmChecker class for the same. The class has to provide two methods "sendMessage" and "receiveMessage".

In the "sendMessage" method, we need to consider two cases:

  1. If the number of messages sent by the sender in a minute is less than the threshold, then send the message, update the message count for the sender, and update the inbox of the receiver.

  2. If the message count exceeds the threshold, then do not send the message and return false.

In the "receiveMessage" method, we need to consider two cases:

  1. If the message is meant for the receiver, then accept the message, update the inbox of the receiver, and return true.

  2. If the message is not meant for the receiver, then do not accept the message and return false.

We need to maintain a hashmap to keep track of the number of messages sent by each user in a minute and a list to maintain the inbox of each user. We also need to maintain the threshold limit for messages sent by a user in a minute.

The time complexity of the "sendMessage" and "receiveMessage" methods would be O(1).

Code:

The implementation of the ConfirmChecker class is given as follows:

class ConfirmChecker { private Map<Integer, Integer> sentMessages; // to keep count of messages sent by each user private List<Map<Integer, String>> inboxes; // to maintain inbox of each user private int maxMessages; // the maximum number of messages that can be sent by a user in a minute

public ConfirmChecker(int maxMessages) {
    this.sentMessages = new HashMap<>();
    this.inboxes = new ArrayList<>();
    this.maxMessages = maxMessages;
}

public boolean sendMessage(int sender, int receiver, String message) {
    // check if sender has exceeded the maximum limit of messages
    if (sentMessages.getOrDefault(sender, 0) >= maxMessages) {
        return false;
    }
    
    // send the message and update the count of messages for the sender
    sentMessages.put(sender, sentMessages.getOrDefault(sender, 0) + 1);
    
    // update the receiver's inbox
    Map<Integer, String> inbox = inboxes.get(receiver);
    if (inbox.size() < maxMessages) {
        inbox.put(sender, message);
        return true;
    }
    
    return false;
}

public boolean receiveMessage(int receiver, int sender, String message) {
    Map<Integer, String> inbox = inboxes.get(receiver);
    
    // check if the message is meant for the receiver
    if (inbox.containsKey(sender) && inbox.get(sender).equals(message)) {
        inbox.remove(sender);
        return true;
    }
    
    return false;
}

}

Users That Actively Request Confirmation Messages Solution Code

1