How hard can be this JavaScript Interview Question from UBER?

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 17

  • @sagarmusic0798
    @sagarmusic0798 11 месяцев назад +1

    A little bit longer but more understandable solution , also it aways give ordered output according on the input list
    const chop = (input, limit) => {
    let res = []
    let i = 0
    let k = 0
    while (i < input.length) {
    let temp = []
    while (k < limit) {
    if (i == input.length) {
    break
    }
    temp.push(input[i])
    k++
    i++
    }
    res.push(temp)
    k = 0
    }
    return res
    }
    function getNamebyId(id, callback) {
    const rRt = Math.floor(Math.random() * 100) + 200;
    setTimeout(() => {
    callback('User' + id)
    }, rRt)
    }
    const performOp = async (choppedInput, iterateeFn) => {
    try {
    let resultList = [];
    for (let i = 0; i < choppedInput.length; i++) {
    for (let j = 0; j < choppedInput[i].length; j++) {
    const result = await new Promise((resolve) => {
    iterateeFn(choppedInput[i][j], resolve);
    });
    resultList.push(result);
    }
    }
    return resultList;
    } catch (error) {
    throw error;
    }
    }
    async function mapLimit(inputs, limit, iterateeFn, callback) {
    const choppedInput = chop(inputs, limit)
    const res = await performOp(choppedInput, iterateeFn)
    callback(res)
    }
    mapLimit([5,3,2,4,1], 2, getNamebyId, (allResults) => {
    console.log("output", allResults)
    })

  • @harishrm5216
    @harishrm5216 9 месяцев назад +1

    Greetings, I have seen you async sequence video where you finally mentioned sequence can be done in async await way also, if we could do the same for this problem it will avoid the complexity of reduce right, I tried but I'm lacking when again durning the async parallel inside that await part, do you have another video for during this with async await way please?

    • @harishrm5216
      @harishrm5216 9 месяцев назад +1

      Figured it out, here is the async await way of yours for sequence order,
      const maxLimit = async (inputs, limit, logicFn, callback) =>{
      const chopped = chop(inputs, limit);
      let finalResult = [];
      for(let input of chopped){
      try{
      let res = await new Promise((resolve, reject) =>{
      let temp = [];
      input.forEach((item) =>{
      getNumberUserId(item, (op) =>{
      temp.push(op)
      if(temp.length === input.length){
      resolve(temp);
      }
      })
      })
      })
      finalResult = [...finalResult, ...res];
      } catch (e){
      console.log(e)
      }
      }
      callback(finalResult );
      }
      Thanks!!

  • @0xatul
    @0xatul Год назад +1

    Great video,

  • @justlifethings4225
    @justlifethings4225 Год назад +2

    Why are you batching here ? Max limit of operations is 2 right. So if you are batching, you are waiting both 1 & 2 to be completed to run 3. 3 can be run right away when either 1 or 2 is completed right ?

    • @Learnersbucket
      @Learnersbucket  Год назад +1

      Ideal it should be but if you read the implementation details here -> caolan.github.io/async/v3/docs.html#mapLimit it say next iteration should take place when previous is done.
      This is a something that can be clarified with the interviewer.

    • @justlifethings4225
      @justlifethings4225 Год назад

      @@Learnersbucket cool 👍 love your content anyways.

    • @neerajkumar-vz9ji
      @neerajkumar-vz9ji 11 месяцев назад +1

      I think @justlifethings4225 is right, this is a little different question that what you have solved. The question clearly mentions max number of operations happening at the same time is the limit. Once one of your promise resolves you have to bring other from the array into process. Not batching.

  • @kotadiyachaitanya7957
    @kotadiyachaitanya7957 Год назад +1

    Bhaiya do you looking for video editor?

  • @vijaynavale3919
    @vijaynavale3919 Год назад +3

    Hi Prashant nice video✓
    When you saw this question first time did you able to solve it on your own ? Or Did you take any reference ?

    • @Learnersbucket
      @Learnersbucket  Год назад +1

      It took me sometime to understand the problem, but once I read about what the method does by reading in Async library, I was able to implement it.

  • @imamansoni
    @imamansoni Год назад +2

    👍👍

  • @aayushgupta1711
    @aayushgupta1711 7 месяцев назад +1

    Would following approach be acceptable? This works correctly.
    class A{
    constructor(numOfProcesses, cb, limit){
    this.limit = limit;
    this.waitingList = [];
    this.size = 0;
    this.numOfProcesses = numOfProcesses;
    this.responses = [];
    this.cb = cb;
    }
    subscribe(fn){
    if(this.size===this.limit) this.waitingList.push(fn);
    else{
    this.size++;
    this.execute(fn);
    }
    }
    async execute(fn){
    const res = await fn();
    this.responses.push(res);
    if(this.waitingList.length>0){
    return this.execute(this.waitingList.shift())
    }else{
    this.size--;
    }
    if(this.responses.length === this.numOfProcesses){
    this.cb(this.responses);
    }
    }
    };
    async function getNameById(id){
    const time = 3000
    return new Promise(res => setTimeout(()=>{
    return res(`User${id}`)
    },time))
    }
    function mapLimit(inputs, limit, iterateeFn, cb){
    const obj = new A(inputs.length,cb,limit);
    inputs.forEach((input)=>{
    obj.subscribe(iterateeFn.bind(this, input))
    })
    }
    mapLimit([1,2,3,4,5],1,getNameById,(answer)=>{
    console.log(answer)
    })