Your Mission
Nexo is building the interactive editor engine for Código con Criterio, which must manage two independent data structures: an action history with undo and redo based on two LIFO stacks (with a maximum capacity) and a pending task queue based on the FIFO principle.
Implement the solve(input) function, which receives:
limiteHistorial: integer between 1 and 20.
operaciones: array of 0 to 100 objects with:
tipo: strictly "REGISTRAR", "DESHACER", "REHACER", "AGREGAR_TAREA", or "PROCESAR_TAREA".
valor: non-empty string of 1 to 60 characters (present only on "REGISTRAR" and "AGREGAR_TAREA").
Operation Rules
Maintain three independent arrays (historial, accionesDeshechas, and colaTareas):
"REGISTRAR":
- Append
valor to the end of historial with push().
- Completely clear
accionesDeshechas.
- If
historial.length > limiteHistorial, remove the oldest action from the front using shift().
- Record
exitosa: true, elemento: valor, and motivo: "OK".
"DESHACER":
- If
historial is empty, fail with exitosa: false, elemento: null, and motivo: "HISTORIAL_VACIO".
- Otherwise, pop the latest action from
historial with pop(), push it onto accionesDeshechas with push(), and record exitosa: true, elemento set to the undone action, and motivo: "OK".
"REHACER":
- If
accionesDeshechas is empty, fail with exitosa: false, elemento: null, and motivo: "SIN_ACCIONES_PARA_REHACER".
- Otherwise, pop the latest action from
accionesDeshechas with pop(), push it onto historial with push() (if historial.length > limiteHistorial, remove the oldest action with shift()), and record exitosa: true, elemento set to the restored action, and motivo: "OK".
"AGREGAR_TAREA":
- Append
valor to the end of colaTareas with push().
- Record
exitosa: true, elemento: valor, and motivo: "OK".
"PROCESAR_TAREA":
- If
colaTareas is empty, fail with exitosa: false, elemento: null, and motivo: "COLA_VACIA".
- Otherwise, remove the first task using
shift() and record exitosa: true, elemento set to the processed task, and motivo: "OK".
Return Value
Return an object with exactly seven properties:
historial: final array of active actions (from oldest to newest).
accionesDeshechas: final array of actions available to redo.
colaTareas: final array of pending tasks.
operacionesExitosas: count of operations with exitosa === true.
operacionesFallidas: count of operations with exitosa === false.
ultimaAccion: last element of historial or null if empty.
registro: array of { tipo, exitosa, elemento, motivo } objects in original order.
Example 1
Input
{
"operaciones": [
{
"tipo": "REGISTRAR",
"valor": "Escribir función"
},
{
"tipo": "REGISTRAR",
"valor": "Agregar test"
},
{
"tipo": "DESHACER"
},
{
"tipo": "REHACER"
},
{
"tipo": "AGREGAR_TAREA",
"valor": "Ejecutar linter"
},
{
"tipo": "PROCESAR_TAREA"
}
],
"limiteHistorial": 3
}Expected result
{
"registro": [
{
"tipo": "REGISTRAR",
"motivo": "OK",
"exitosa": true,
"elemento": "Escribir función"
},
{
"tipo": "REGISTRAR",
"motivo": "OK",
"exitosa": true,
"elemento": "Agregar test"
},
{
"tipo": "DESHACER",
"motivo": "OK",
"exitosa": true,
"elemento": "Agregar test"
},
{
"tipo": "REHACER",
"motivo": "OK",
"exitosa": true,
"elemento": "Agregar test"
},
{
"tipo": "AGREGAR_TAREA",
"motivo": "OK",
"exitosa": true,
"elemento": "Ejecutar linter"
},
{
"tipo": "PROCESAR_TAREA",
"motivo": "OK",
"exitosa": true,
"elemento": "Ejecutar linter"
}
],
"historial": [
"Escribir función",
"Agregar test"
],
"colaTareas": [],
"ultimaAccion": "Agregar test",
"accionesDeshechas": [],
"operacionesExitosas": 6,
"operacionesFallidas": 0
}Example 2
Input
{
"operaciones": [
{
"tipo": "DESHACER"
},
{
"tipo": "REGISTRAR",
"valor": "Paso 1"
},
{
"tipo": "REGISTRAR",
"valor": "Paso 2"
},
{
"tipo": "REGISTRAR",
"valor": "Paso 3"
},
{
"tipo": "PROCESAR_TAREA"
}
],
"limiteHistorial": 2
}Expected result
{
"registro": [
{
"tipo": "DESHACER",
"motivo": "HISTORIAL_VACIO",
"exitosa": false,
"elemento": null
},
{
"tipo": "REGISTRAR",
"motivo": "OK",
"exitosa": true,
"elemento": "Paso 1"
},
{
"tipo": "REGISTRAR",
"motivo": "OK",
"exitosa": true,
"elemento": "Paso 2"
},
{
"tipo": "REGISTRAR",
"motivo": "OK",
"exitosa": true,
"elemento": "Paso 3"
},
{
"tipo": "PROCESAR_TAREA",
"motivo": "COLA_VACIA",
"exitosa": false,
"elemento": null
}
],
"historial": [
"Paso 2",
"Paso 3"
],
"colaTareas": [],
"ultimaAccion": "Paso 3",
"accionesDeshechas": [],
"operacionesExitosas": 3,
"operacionesFallidas": 2
}