Pergunta de entrevista da empresa Miro

Task Runner with concurrent limit [Javascript] The problem was to complete the add function for Runner class. concurrent limit will determine the number of task Runner can run in parallel. If the concurrent limit is reached by Runner, rest of the tasked should be queued and only to be executed once the first set of task are completed. ------------------------------------------------------------- class Runner { constructor(concurrent) { } add(task) { // Add your code here. } } function task(x) { return function() { return new Promise((resolve, _) => { setTimeout(() => { console.log('task completed', x); resolve(); }, 2000); }) } } runner = new Runner(3); runner.add(task(2)) runner.add(task(2)) runner.add(task(2)) runner.add(task(4)) runner.add(task(4)) runner.add(task(4)) runner.add(task(6)) runner.add(task(6)) runner.add(task(6))

Resposta da entrevista

Sigiloso

5 de jan. de 2022

Problem looks really simple. Out of nervousness I was not able to complete the solution on time. class Runner { constructor(concurrent) { this.queue = []; this.count = 0; this.concurrent = concurrent; } add(task) { this.queue.push(task); this.run(); } run() { while (this.count { this.count--; this.run(); }); } } }