- 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.
Write a function that calls
I tried to write a code that goes over the arrays in
I'll be happy to hear any suggestion for solving this problem so I could learn from it.
Thank you!
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!