Amazon Interview Question
Country: United States
Interview Type: In-Person
JS Solution:
function getTopBooks(friends) {
const friends = {
2: [10,20,30,40,50,60],
3: [10,70,50],
4: [20, 10, 40, 50],
5: [20, 30]
};
const book_count = {};
Object.keys(friends).map(function(key) {
let bookList = friends[key];
bookList.forEach(bookElement => {
book_count[bookElement] = book_count[bookElement] ? book_count[bookElement] + 1 : 1;
});
});
let keysSorted = Object.keys(book_count).sort(function(a,b){return book_count[b] - book_count[a]});
return keysSorted.slice(0, 10)
}
function getTopBooks(friends) {
const friends = {
2: [10,20,30,40,50,60],
3: [10,70,50],
4: [20, 10, 40, 50],
5: [20, 30]
};
const book_count = {};
Object.keys(friends).map(function(key) {
let bookList = friends[key];
bookList.forEach(bookElement => {
book_count[bookElement] = book_count[bookElement] ? book_count[bookElement] + 1 : 1;
});
});
let keysSorted = Object.keys(book_count).sort(function(a,b){return book_count[b] - book_count[a]});
return keysSorted.slice(0, 10)
}
}
- alir2t2 June 04, 2018