This question was asked in Meta’s frontend interview.
The problem statement reads as – Implement an engine that process async callbacks using JavaScript. Your task is to provide implementation of the class QueueCallbacks to meet all the condition:
Constructor requirements
You should provide the implementation of the constructor and process methods, Do not change the names of these methods or the number of arguments they receive.
- The constructor method should receive an optional string., The value of the string will be responsible for the order in which callbacks stored in the queue are processes.
- The only non-empty value it can receive in the string LIFO(Last in first out). the default order of processing callbacks in the queue will be **FIFO(**First in first out).
Process method requirements
The process method receive a single async function that should be executes by following the algorithm described below:
- If there is currently no async function being executed by the class, the received callback method should be executed immediately.
- If there is currently only one async function currently being executed the callback method should be executed immediately as well.
- If there are two async function currently being executed the next callback method should be put into the queue.
- After one of the currently executing async function is finished.
- When there were no argument passed to the constructor the first callback method that was pushed into the queue should be executed (First in first out).
- When the argument passed to the constructor was LIFO, the last callback in the queue should be executed.
- If there are more than 6 callbacks in the queue discard any extra callbacks.
- If there are more than 3 callbacks in the queue, follow FIFO if no argument is passed to constructor.
Solution
If you read the problem statements carefully you would be implement the algorithm step by step. This is how we are going to solve this:
- We will create a constructor function with two methods,
process a public method and executeNext a private method.
- We will use three variables, first to track the order, set the default as FIFO, second to hold the callbacks queue, and third to track the number of executions happening.