Javascript interview question that I couldn't solve

Joined
Jun 6, 2024
Messages
1
Reaction score
0
I had a technical interview and I couldn't solve the exersice they challenged me with. Can someone please explain how to solve this?

Givan an object of users. Each user can belong to one group or more.

Code:
const users = {
    1: {
        id: 1,
        email: '[email protected]',
        teams: []
    },
    2: {
        id: 2,
        email: '[email protected]',
        teams: [1,2]
    },
    3: {
        id: 3,
        email: '[email protected]',
        teams: [2,4]
    },
    4: {
        id: 4,
        email: '[email protected]',
        teams: [1,5]
    },
}


Code:
const msg = {
    text: "Hi",
    sender: 2,
    mentions: {
        users: [1,3],
        groups: [3,4]
    }
}

Code:
function sendNotification(text, email) {
    console.log(`send email to ${email}`);
}

Write a function that calls sendNotification fucntion in order to send a message to all the users and the teams mentioned in the msg.mentions object. You can't change anything in the given users object and the sendNotification functions.

Code:
function x(msg) {
  // implement your code here.
}


I tried to write a code that goes over the arrays in msg.netions.users and msg.netions.users and check for the user's email and its group, but the time complexity is bad in this approach.

Code:
function x(msg) {
    for (const userId in msg.mentions.users) {
        sendNotification(msg.text, users[userId].email);
    }
    for (let group in msg.mentions.groups) {
        for (const property in users) {
            for (const team in users[property].teams) {
                if (group === team) {
                    sendNotification(msg.text, users[property].email);
                }
            }
        }
    }
}



I'll be happy to hear any suggestion for solving this problem so I could learn from it.

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top