Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
Node JS Concurrency Handling Coding Interview Question

I attended a NodeJS coding interview. Got below code which executes asynchronously from different browsers(to be assumed). Our solution needs to lock the execution of function if the update by ID is same but called from different place (say browser). And then release the lock for next request's execution.

Here no changes to be made for the mentioned code below.

async function update(id, data) {
    console.log(`start --> id:${id}, data:${data}`);
    await randomDelay(); //update is happening here
    console.log(`end --> id:${id}, data:${data}`);
}

//=============================================================================
//================= Don't change anything below ===============================
//=============================================================================

//---- update() is getting called from many places ----
update(1, "browser 1");
update(1, "browser 2");

//========================= Utility functions ===================================
//========================= Don't change any here================================

async function sleep(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve(), ms);
    });
}

async function randomDelay() {
    const randomTime = Math.round(Math.random() * 1000);
    return sleep(randomTime);
}

This will give an output like below.

start --> id:1, data:browser 1
start --> id:1, data:browser 2
end --> id:1, data:browser 1
end --> id:1, data:browser 2

The expected answer is

start --> id:1, data:browser 1
end --> id:1, data:browser 1
start --> id:1, data:browser 2
end --> id:1, data:browser 2

Please note the comments in code "Don't change anything below". What would be the possible solution?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could use a hash table of queues keyed by ID so only jobs with the same ID run consecutively, otherwise they run concurrently.

let hash = {};
class Queue {
  constructor() {
    this.isBusy = false;
    this.jobs = [];
  }

  push(jobFn) {
    return new Promise((resolve) => {
      this.jobs.push({
        jobFn,
        resolve
      });
      this.next();
    });
  }

  next() {
    if (this.isBusy || this.jobs.length === 0) return;
    this.isBusy = true;
    let currJob = this.jobs.shift();
    return currJob.jobFn().then((data) => {
      currJob.resolve(data);
      this.isBusy = false;
      this.next();
    });
  }
}

async function update(id, data) {
  const updateFn = async () => {
    console.log(`start --> id:${id}, data:${data}`);
    await randomDelay(); //update is happening here
    console.log(`end --> id:${id}, data:${data}`);
  };
  if (id in hash) {
    hash[id].push(updateFn);
  } else {
    hash[id] = new Queue(updateFn);
    hash[id].push(updateFn);
  }
}

//=============================================================================
//================= Don't change anything below ===============================
//=============================================================================

//---- update() is getting called from many places ----
update(1, "browser 1");
update(1, "browser 2");
update(2, "browser 1");
update(2, "browser 2");
update(1, "browser 3");
update(1, "browser 4");

//========================= Utility functions ===================================
//========================= Don't change any here================================

async function sleep(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(), ms);
  });
}

async function randomDelay() {
  const randomTime = Math.round(Math.random() * 1000);
  return sleep(randomTime);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

This solution works for just two consecuence calls with different data as you expected, so I am working on extending it but for now, I hope will give you a good vision of how it should be implemented

const callStack = []
async function update(id, data) {
    const stackLen = callStack.length;
    let currentIndex;

    if (stackLen) {
        let currentCall = callStack[stackLen - 1];
        if (currentCall.start == true && currentCall.await == true) {
            setImmediate(() => update(id, data))
            return;
        }
        currentIndex = stackLen - 1;
        if (currentCall.args[0] == id && currentCall.args[1] !== data) {
            
            if (currentCall.await === true) {
                currentCall.start = true;
                update(id, data)
                return;
            }
        }
    } else {
        callStack.push({ args: [...arguments], await: true })
        currentIndex = 0;
    }
    console.log(`start --> id:${id}, data:${data}`);
    await randomDelay(); //update is happening here
    console.log(`end --> id:${id}, data:${data}`);
    callStack[currentIndex].await = false;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda