I was asked in an interview to process two multidimensional arrays into one object with the format similar to:
{
'sally': {
id: 1,
metrics: {
sales: 6000,
missed: 0
}
},
'bob': {
id: 2,
metrics: {
sales: 1000,
missed: 0
}
},..
}
However, I was unable to figure it out. I've tried doing nested for-loops, but the problem with this is I'm worried about execution time. According to the interviewer, it should have an execution time of O(n) and if I understand Big O correctly, a nested for-loop would have an execution time of around O(n^2). Here is the code I have so far:
// returns a dictionary
let testIAteShitOn = (requested_user = false, requested_metric = false) => {
// is the array always organized the same?
let users = [
[1, "Sally"],
[2, "Bob"],
[3, "George"],
];
// same here, is this always organized the same?
// are there only ever 2 metrics?
let metrics = [
[1, "sales", 5000],
[1, "sales", 1000],
[3, "missed", 1000],
[2, "sales", 1000],
];
for (let i = users.length - 1; i >= 0; i--) {
let id = users[i][0];
let data = findDiamond(id, metrics);
}
return obj;
}
let findDiamond = (id, array) => {
return array.filter((item) => item[0] == id);
}
I would appreciate any help on this and advice on where and how to best learn algorithms, data structures, and Big O.