Simulate Task Processing with Undo
Flipkart's order fulfillment system processes various tasks, but sometimes errors or changes require rolling back recent operations. This problem simulates a simplified task processing system that handles incoming tasks in order, and allows for undoing the most recently completed task. Your task is to implement the core logic for this system.
Implement the simulateProcessingSystem function that takes an array of operations. The system should maintain an internal state to handle these operations:
ADD <task_id>: Adds a task identified by task_id to a queue for pending processing.PROCESS: Takes the next available task from the pending queue and marks it as completed. This completed task should be available for an UNDO operation.UNDO: Reverts the last PROCESS operation. If an UNDO occurs, the task that was just processed is "un-processed" and effectively removed from the "completed" state. If there are no completed tasks to undo, this operation has no effect.The function should return an array of results, where each result corresponds to an operation in the input array.
Function Signature:
function simulateProcessingSystem(operations: string[]): (string | null)[]
Input:
operations: An array of strings. Each string represents an operation.
"ADD <task_id>": task_id is a string identifier for the task (e.g., "T101")."PROCESS": No additional parameters."UNDO": No additional parameters.Output:
(string | null)[]: An array of results corresponding to each operation.
"ADD <task_id>": Return null."PROCESS": Return the task_id of the processed task. If the pending queue is empty, return null."UNDO": Return the task_id of the task that was undone. If there are no tasks available to undo, return null.task_ids.PROCESS operations always take from the front of the pending queue (FIFO).UNDO operations always revert the last task that was PROCESSed (LIFO).PROCESS is called on an empty queue, it processes nothing and returns null. This "null processing" itself cannot be undone.UNDO is called when no tasks have been PROCESSed or all processed tasks have been undone, it does nothing and returns null.Examples
Example 1
Input:
operations = ["ADD T1", "ADD T2", "PROCESS", "PROCESS", "UNDO", "PROCESS"]
Output:
[null,null,"T1","T2","T2",null]
Note:
Example 2
Input:
operations = ["PROCESS", "UNDO", "ADD T1", "PROCESS", "UNDO", "UNDO"]
Output:
[null,null,null,"T1","T1",null]
Note:
Example 3
Input:
operations = ["ADD A", "PROCESS", "ADD B", "ADD C", "PROCESS", "UNDO", "PROCESS", "UNDO", "UNDO"]
Output:
[null,"A",null,null,"B","B","C","C","A"]
Note:
Constraints
1 <= operations.length <= 10^5task_id will be a non-empty string, consisting of alphanumeric characters.task_id mentioned in an ADD operation will be unique within the context of tasks currently awaiting processing or recently processed.ADD, PROCESS, and UNDO operations, handling edge cases like empty queues/stacks?Loading editor...