Interview Question
Front End Developer Interview
-
Metaby phone: what is a difference between Object and Array, call and apply, what is DOM structure, what is time complexity of traversing through the DOM, other very basic questions. online interview: first question is about data structure, given two arrays, one of them is a map to another, you need to traverse and exclude from array objects according to the map, in general they expect from you to build an map object from key+value but you have to use ES6 MAP where key can be stored as object and not as string or ASCII coded string. The second question was about setTimeout browser api, you will be asked to clean all timeouts, the answer is to override setTimeout and store all of them in stuck and clean them if needed.
Interview Answers
2 Answers
// create new TimeOut, which gives new id then clear all till that Id var newTimeOut = setTimeout[() => {}, 1); for (var i=0 ; i <= newTimeOut ; i++ ) { clearTimeout(i); }
Nikhil on
let timeOuts = []; const realSetTimeout = window.setTimeout; const realClearTimeout = window.clearTimeout; window.setTimeout = function (callback, time, ...args) { const id = realSetTimeout( () => { callback(); delete timeOuts[timeOuts.indexOf(id)]; }, time, ...args ); timeOuts.push(id); return id; }; window.clearTimeout = function (id) { realClearTimeout(id); delete timeOuts[timeOuts.indexOf(id)]; }; function clearAllTimeout() { timeOuts.forEach((id) => realClearTimeout(id)); timeOuts = []; }
Implementation for clearAllTimeout on