Amazon Interview Question
Front-end Software EngineersCountry: United States
Interview Type: Phone Interview
function enqueue(q, data) {
if (!isArray(q) || !q) {
throw new Error('Invalid');
}
q.push(data);
return q;
}
function dequeue(q) {
if (!isArray(q) || !q) {
throw new Error('Invalid');
}
return q.shift();
}
function isArray(inp){
return Object.prototype.toString.call(inp).splice(8, -1) === 'Array';
}
Use array to implement the deque. enqueue at end and dequeue at start.
- Ani July 08, 2012