// Asked in Uber and Rippling
// https://leetcode.com/discuss/interview-experience/2074287/uber-frontend-phone-screen
// inputs: An array containing the inputs to be processed.
// maxlimit: The maximum number of concurrent operations that can be executed
// iterateeFn: An asynchronous function that is called for each input to
// generate the corresponding output. It accepts two arguments:
// input: The current input being processed.
// callback: A function that is invoked when the processing of the input is
// complete. It receives the processed output as its argument.
// callback: A function that is called once all inputs have been processed.
// It receives an array of the final outputs.
// function getNameById(id, callback) {
// const randomRequestTime = Math.floor(Math.random() * 100) + 2000;
// console.log("randomRequestTime", randomRequestTime, id);
// }, randomRequestTime);
// function mapLimit(inputs, maxLimit, iterateeFn, callback) {
// mapLimit([1,2,3,4,5], 3, getNameById, (allResults) => {
// console.log('output:', allResults)
// // ["User1", "User2", "User3", "User4", "User5"]
function getNameById(id, callback) {
// simulating async request
const randomRequestTime = Math.floor(Math.random() * 100) + 4000;
console.log("randomRequestTime", randomRequestTime, id);
function mapLimit(inputs, maxLimit, iterateeFn, callback) {
const processNext = (id) => {
if(result.length == inputs.length ) {
if(currentPtr < inputs.length && currentLimit < maxLimit) {
iterateeFn(inputs[currentPtr++], processNext);
while(currentPtr < inputs.length && currentLimit < maxLimit) {
iterateeFn(inputs[currentPtr++], processNext)
mapLimit([1,2,3,4,5], 2, getNameById, (allResults) => {
console.log('output:', allResults)