Coding
Software Engineer
Create a command history that supports both ordinary undo and undo restricted to a particular tag.
Implement the CommandLog class:
CommandLog() creates a log with no commands.void execute(string command, string[] tags) records a command together with its tags. A command may be associated with no tags or with multiple tags.string undo(string tag) deletes and returns a single command from the log.
tag is omitted or is null, select the newest command that remains active.Additional rules:
Choose a data structure that keeps every operation efficient across as many as 10^5 calls.
Example 1:
Input:
`["CommandLog","execute","execute","execute","undo","undo","undo"]
[[],["A",["x"]],["B",["y"]],["C",["x","y"]],[null],["x"],["y"]]`
Output: [null,null,null,null,"C","A","B"]
Explanation: The null undo takes the newest command overall, C. The subsequent x and y undos then select A and B, respectively.
Example 2:
Input:
`["CommandLog","execute","execute","execute","undo","undo","undo"]
[[],["alpha",[]],["beta",["prod"]],["gamma",[]],["prod"],[null],[null]]`
Output: [null,null,null,null,"beta","gamma","alpha"]
execute and undo calls is at most 10^5.execute call, 0 <= tags.length <= 10.1 <= command.length <= 100, and command is an opaque identifier.1 <= tag.length <= 20.undo call is valid; a command available for that call always exists.var CommandLog = function() {
};
/**
* @param {string} command
* @param {string[]} tags
* @return {void}
*/
CommandLog.prototype.execute = function(command, tags) {
};
/**
* @param {string | null} tag
* @return {string}
*/
CommandLog.prototype.undo = function(tag) {
};