Effective Configuration Value
In Salesforce, managing configurations often involves layered settings where values can be temporarily overridden and later reverted. Consider a scenario in a custom metadata or feature toggle system where administrators can define a base configuration value, apply temporary overrides, and then "roll back" to previous states. This problem simulates processing such a sequence of configuration changes to determine the final, effective configuration value.
Implement a function getFinalConfiguration that processes a list of configuration operations and returns the final effective configuration value. The operations modify a single string-based configuration value.
The operations are:
SET X: Sets the current effective configuration value to X. This operation directly changes the active value.PUSH X: Overrides the current effective configuration value with X. The previous effective value (if any) is saved onto a stack, allowing for future reversion.POP: Reverts the current effective configuration value to the last saved value on the stack. If the stack is empty, this operation does nothing.After all operations are processed, return the final effective configuration value. If no SET or PUSH operation ever occurred, or if all values were reverted such that the configuration is effectively unset, return null.
Input:
operations: A string[] representing a sequence of configuration commands. Each string will be in one of the following formats:
"SET X": where X is a single uppercase English letter (e.g., "SET A")."PUSH X": where X is a single uppercase English letter (e.g., "PUSH B")."POP": (no additional arguments).Output:
string | null: The final effective configuration value after processing all operations. Return null if the configuration is effectively unset.null.PUSH X operation occurs, if the current effective_value is null, null should be pushed onto the stack.POP is called on an empty stack, the effective_value remains unchanged.X are always single uppercase English letters (A-Z).Examples
Example 1
Input:
operations = ["SET A", "PUSH B", "POP", "PUSH C", "POP"]
Output:
"A"
Note:
effective_value = null, stack = []SET A: effective_value = "A"PUSH B: Push "A". stack = ["A"], effective_value = "B"POP: Pop "A". stack = [], effective_value = "A"PUSH C: Push "A". stack = ["A"], effective_value = "C"POP: Pop "A". stack = [], effective_value = "A"
Final effective_value is "A".Example 2
Input:
operations = ["PUSH X", "PUSH Y", "POP", "SET Z", "POP"]
Output:
null
Note:
effective_value = null, stack = []PUSH X: Push null. stack = [null], effective_value = "X"PUSH Y: Push "X". stack = [null, "X"], effective_value = "Y"POP: Pop "X". stack = [null], effective_value = "X"SET Z: effective_value = "Z"POP: Pop null. stack = [], effective_value = null
Final effective_value is null.Example 3
Input:
operations = ["POP", "POP", "SET A"]
Output:
"A"
Note:
effective_value = null, stack = []POP: Stack empty, do nothing. effective_value = null, stack = []POP: Stack empty, do nothing. effective_value = null, stack = []SET A: effective_value = "A"
Final effective_value is "A".Constraints
0 <= operations.length <= 10^5operation string is well-formed according to the specified formats ("SET X", "PUSH X", "POP").X will always be a single uppercase English letter (A-Z).Loading editor...