Frontend School

Effective Configuration Value

DSA
medium

Effective Configuration Value

Medium
Stack
String Parsing

Overview

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.

What you need to do / Task

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:

  1. SET X: Sets the current effective configuration value to X. This operation directly changes the active value.
  2. 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.
  3. 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 & output

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.

Clarifications

  • The initial effective configuration value is null.
  • When a PUSH X operation occurs, if the current effective_value is null, null should be pushed onto the stack.
  • If POP is called on an empty stack, the effective_value remains unchanged.
  • The values X are always single uppercase English letters (A-Z).

Examples

1

Example 1

Input:

operations = ["SET A", "PUSH B", "POP", "PUSH C", "POP"]

Output:

"A"

Note:

  1. Initial: effective_value = null, stack = []
  2. SET A: effective_value = "A"
  3. PUSH B: Push "A". stack = ["A"], effective_value = "B"
  4. POP: Pop "A". stack = [], effective_value = "A"
  5. PUSH C: Push "A". stack = ["A"], effective_value = "C"
  6. POP: Pop "A". stack = [], effective_value = "A" Final effective_value is "A".
2

Example 2

Input:

operations = ["PUSH X", "PUSH Y", "POP", "SET Z", "POP"]

Output:

null

Note:

  1. Initial: effective_value = null, stack = []
  2. PUSH X: Push null. stack = [null], effective_value = "X"
  3. PUSH Y: Push "X". stack = [null, "X"], effective_value = "Y"
  4. POP: Pop "X". stack = [null], effective_value = "X"
  5. SET Z: effective_value = "Z"
  6. POP: Pop null. stack = [], effective_value = null Final effective_value is null.
3

Example 3

Input:

operations = ["POP", "POP", "SET A"]

Output:

"A"

Note:

  1. Initial: effective_value = null, stack = []
  2. POP: Stack empty, do nothing. effective_value = null, stack = []
  3. POP: Stack empty, do nothing. effective_value = null, stack = []
  4. SET A: effective_value = "A" Final effective_value is "A".

Constraints

Constraints & assumptions

  • 0 <= operations.length <= 10^5
  • Each operation 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).
  • The system should be efficient for a large number of operations.

What interviewers evaluate

  • Correctness: The solution correctly handles all operation types, stack manipulations, and edge cases (e.g., empty stack, initial null state).
  • Efficiency: The algorithm processes operations in an optimal time complexity (linear time) and uses appropriate space complexity.
  • Code Quality: The code is clean, readable, well-structured, and follows good programming practices.
  • Problem-Solving: Demonstrates a clear understanding of stack data structure and its application to manage state and reverts.

Loading editor...