You need Login/Signup to run/submit the code.
How to implement custom map function with limit on number of operations? | Paytm Frontend Interview Question | JavaScript Interview Questions | Frontend Problem Solving
@Yomesh Gupta
In this question, you need to implement a custom mapLimit
function that takes 4 arguments
inputs
: An array of inputslimit
: The maximum number of operations at any given time.iterateeFn
: The async function that should be called with each input to generate the corresponding output. It will have two arguments:input
: The input being processedcallback
: A function that will be called when the input is finished processing. It will be provided with one argument, the processedoutput
.
callback
: A function that should be called with the array of outputs once all inputs have been processed.
At any given point, your program can make max 2 calls i.e. at any given point your program can process 1, 2
or 2, 3
or so on user ids.
function getUserById(id, callback) {
// simulating async request
const randomRequestTime = Math.floor(Math.random() * 100) + 200;
setTimeout(() => {
callback("User" + id)
}, randomRequestTime);
}
function mapLimit(inputs, limit, iterateeFn, callback) {
// implement here
}
mapLimit([1,2,3,4,5], 2, getUserById, (allResults) => {
console.log('output:', allResults) // ["User1", "User2", "User3", "User4", "User5"]
})